How to exclude parts of output from derivation

What would be the easiest way to make a new derivation that contains a subset of the output from a package? For example, say that nix-build --no-link '<nixpkgs>' -A some-package produced the output:

/nix/store/<hash>-some-package-0.0.1/:
some/things I want
some/junk that I don't want polluting my profile

I want a new “package” with just the things I want linking to the old package, so I can install it without getting the junk. How would I do that?

Alternatively, I would want some other method to just install parts of a package to my profile.

What you will probably want to do is add a postInstall hook which will remove the unwanted files

newDerivation = oldDerviation.overrideAttrs(oldAttrs: {
  postInstall = ''
    rm some/junk
  '';
});

any phase after installPhase should work to do this, so postFixup should also work if postInstall doesn’t get called (some people override the default installPhase, and then it’s on them to add back the runHook preInstall/postInstall hooks, many people don’t).

2 Likes

Thanks! Does this mean that I can’t use the cached version of the build, since I am modifying the derivation? It would be nice to be able to download it from cache.nix.org instead of building it myself.

yes, a override would create a new derivation, then you would need to build it.

you could probably just grab the files that you care about in a new derivation as well, although this probably isn’t recommended as you would be heavily dependent on the layout of another derviation:

{thePackage}:

mkDerivation rec {
 ...
  inherit (thePackage) buildInputs propagatedBuildInputs;
  src = null;
  dontBuild = true;
  installPhase = ''
    # just copy the files
    cp -r ${thePackage}/{bin,doc,share,lib} ${out}
  '';
   ...
}

It would probably be simpler to use buildEnv instead, specifying pathsToLink to only link the particular paths you want.

That would be ideal if he cares only about the files in his user environment. However, if his main intent is to not have these files in his hardisk then he’ll still have the original version of the package in his store…