NixOS / nix

Nix, the purely functional package manager
https://nixos.org/
GNU Lesser General Public License v2.1
11.59k stars 1.45k forks source link

Allow direct access to hash rewrite functionality #6832

Open tejing1 opened 1 year ago

tejing1 commented 1 year ago

Is your feature request related to a problem? Please describe. Say I want to modify the files in the chromium derivation, but compiling it is quite painful, and not necessary for the change I want to make, so rather than using overrideAttrs, which would compile it, I make a derivation which copies the substitutable output, then modifies it. This is almost a perfectly good solution. The only fly in the ointment is self references. However, nix already has a solution to this problem internally, since it's needed for ca-derivations... we just need to expose it to the nix language.

Describe the solution you'd like Expose the hash-rewriting functionality of nix as a builtin. In particular, it needs to be able to rewrite to the (as yet unknown) resulting derivation's hash.

Example use:

let
  alterDerivation = pkg: inputs: commands:
    builtins.hashrewrite { from = pkg; toresult = true; } (pkgs.stdenv.mkDerivation {
      inherit (pkg) pname version meta;

      src = "${pkg}";

      nativeBuildInputs = inputs;

      dontUnpack = true;
      dontPatch = true;
      dontConfigure = true;
      dontBuild = true;
      installPhase = ''
        cp -a $src $out
        ${commands}
      '';
    });
in
alterDerivation pkgs.foo [ pkgs.gnused ] "sed -i -e 's/bar/baz/' $out/some/file"

Describe alternatives you've considered

tejing1 commented 1 year ago

It might also be useful to allow simultaneous rewriting of multiple derivations, particularly for dealing with certain kinds of circular dependencies in other package managers?

tejing1 commented 6 months ago

Recording this here since it may be useful to people searching for related issues: pkgs.replaceDependency is able to replace store paths deep in a closure, rewriting all the intermediate nodes in the DAG in the process, without recompiling anything. Nixos also has an option based on this functionality, system.replaceRuntimeDependencies.

pkgs.replaceDependency does use IFD, however, and as far as I know, it can't be used to alter a store path while rewriting self-references within that store path. A more comprehensive solution built into the nix language is still preferable.