Nix Cookbook: Difference between revisions
mNo edit summary |
→Vulnerabilities: Add an actual link to vulnix |
||
(9 intermediate revisions by 3 users not shown) | |||
Line 4: | Line 4: | ||
=== Reclaim space on Nix install? === | === Reclaim space on Nix install? === | ||
{{META Box|TL&DR:|<code>nix-collect-garbage --delete-older-than 7d</code>}} | |||
==== Remove old generations ==== | ==== Remove old generations ==== | ||
When you make changes to your system, Nix creates a new system [[generation]]. All of the changes to the system since the previous generation are stored there. Old generations can add up and will not be removed automatically by default. You can see your generations with: | When you make changes to your system, Nix creates a new system [[generation]]. All of the changes to the system since the previous generation are stored there. Old generations can add up and will not be removed automatically by default. You can see your saved system generations with: | ||
< | <syntaxhighlight lang="shell-session"> | ||
# nix-env --profile /nix/var/nix/profiles/system --list-generations | |||
</ | </syntaxhighlight> | ||
To keep just your current generation and the two older than it: | To keep just your current generation and the two older than it: | ||
< | <syntaxhighlight lang="shell-session"> | ||
# nix-env --profile … --delete-generations +3 | |||
</ | </syntaxhighlight> | ||
To remove all but your current generation: | To remove all but your current generation: | ||
< | <syntaxhighlight lang="shell-session"> | ||
# nix-env --profile … --delete-generations old | |||
</ | </syntaxhighlight>Apart from the system profile in <code>/nix/var/nix/profiles/system</code>, every user has profiles for their user environment and channels. The operations above may be repeated for those profiles. By default, they are located at <code>~/.nix-profile</code> and <code>~/.nix-defexpr/channels</code>. For more information on profile locations, see [https://nix.dev/manual/nix/stable/command-ref/nix-env#files Nix Manual - nix-env - Files]. | ||
===== Generation trimmer script ===== | ===== Generation trimmer script ===== | ||
Line 125: | Line 126: | ||
</syntaxHighlight> | </syntaxHighlight> | ||
== Deprecating a specific input parameter in mkDerivation-style packages == | |||
Sometimes we want to rename some input parameter in . | |||
E.G. an option `withX` that enables the X11 GUI for a certain app: | |||
<syntaxHighlight lang="nix"> | |||
{ | |||
/*. . .*/ | |||
withX ? true, | |||
/*. . .*/ | |||
}: | |||
stdenv.mkDerivation { /* . . . */} | |||
</syntaxHighlight> | |||
Suppose that a new version of this package features a more agnostic GUI that can be linked to X11, GTK, Qt etc. | |||
Because of it, `withX` is no longer a descriptive name for this functionality. | |||
However, renaming the parameter is dangerous, because other functions that call this function expect this parameter. | |||
The problem becomes more pronounced when in conjunction with custom, third-party overlays. | |||
The solution is, roughly, to emit a warning about the old parameter being used, reporting the user to the new parameter: | |||
<syntaxHighlight lang="nix"> | |||
{ | |||
/*. . .*/ | |||
withX ? null, | |||
withGui ? | |||
if (withX != null) then | |||
lib.warn '' | |||
withX is deprecated and will be removed in the next release; | |||
use withGui instead. | |||
'' withX | |||
else | |||
true | |||
/*. . .*/ | |||
} | |||
</syntaxHighlight> | |||
With this warning, consumers will have time to patch their codes. | |||
== Bulk pre-download all dependencies of a package == | |||
Sometimes we need to download all source dependencies of a package. | |||
E.G. a long build is being planned, so we first download all needed files, so that after that we only need to worry about local (non-Internet) issues. | |||
Here is a one-liner for downloading all the source dependencies of a package (thanks Eelco Dolstra!): | |||
<syntaxHighlight lang="shell"> | |||
$> nix-store -r $(grep -l outputHash $(nix-store -qR $(nix-instantiate '<nixpkgs>' -A bochs) | grep '.drv$')) | |||
</syntaxHighlight> | |||
Let's dissect this: | |||
<syntaxhighlight lang="sh" line="1"> | |||
## instantiate bochs into `.drv` files and print the filenames; | |||
instantiate=$(nix-instantiate '<nixpkgs>' -A bochs) | |||
## print all references/requirements, filtering the .drv files (which is where static derivations live) | |||
requirements=$(nix-store -qR $instantiate | grep '.drv$') | |||
## keep only the source derivations, since those will have a predefined hash of the output | |||
sources=$(grep -l outputHash $requirements) | |||
## realize those derivations, downloading all sources and storing them in the nix store | |||
nix-store -r $sources | |||
</syntaxhighlight> | |||
After that, all sources will be locally stored! | |||
Source: [https://web.archive.org/web/20160829181620/http://lists.science.uu.nl/pipermail/nix-dev/2013-January/010438.html nix-dev thread] | |||
== Wrapping packages == | == Wrapping packages == | ||
Line 312: | Line 388: | ||
=== Vulnerabilities === | === Vulnerabilities === | ||
See | See [https://github.com/nix-community/vulnix vulnix]. | ||
[[Category:nix]] | [[Category:nix]] | ||
[[Category:Cookbook]] | [[Category:Cookbook]] |