FAQ: Difference between revisions

imported>Makefu
import from nixos-users
imported>Fadenb
m Syntax highlighting + indentation
Line 15: Line 15:
If you have a lot of dependencies, you may want to write a nix expression that includes your dependencies so that you can simply use <code>nix-shell</code> rather than writing out each dependency every time or keeping your development environment in your shell history. A minimal example looks like this:
If you have a lot of dependencies, you may want to write a nix expression that includes your dependencies so that you can simply use <code>nix-shell</code> rather than writing out each dependency every time or keeping your development environment in your shell history. A minimal example looks like this:


<pre class="nix"># default.nix
<syntaxhighlight lang="nix"># default.nix
with import &lt;nixpkgs&gt; {};
with import <nixpkgs> {};
stdenv.mkDerivation {
stdenv.mkDerivation {
     name = &quot;dev-environment&quot;; # Probably put a more meaningful name here
     name = "dev-environment"; # Probably put a more meaningful name here
     buildInputs = [ pkgconfig zlib ];
     buildInputs = [ pkgconfig zlib ];
}</pre>
}</syntaxhighlight>
=== Why does it work like that? ===
=== Why does it work like that? ===


Line 27: Line 27:
== How to keep build-time dependencies around / be able to rebuild while being offline? ==
== How to keep build-time dependencies around / be able to rebuild while being offline? ==


<pre class="nix"># /etc/nixos/configuration.nix
<syntaxhighlight lang="nix"># /etc/nixos/configuration.nix
{ config, pkgs, lib, ... }:
{ config, pkgs, lib, ... }:
{
{
Line 34: Line 34:
     gc-keep-derivations = true
     gc-keep-derivations = true
   '';
   '';
}</pre>
}</syntaxhighlight>
Check 'man configuration.nix' for these options. Rebuild for these options to take effect:
Check 'man configuration.nix' for these options. Rebuild for these options to take effect:


<pre class="console">nixos-rebuild switch</pre>
<syntaxhighlight lang="bash">nixos-rebuild switch</syntaxhighlight>
List all store paths that form the system closure and realise them:
List all store paths that form the system closure and realise them:


<pre class="console">nix-store -qR $(nix-instantiate /etc/nixos/nixos -A system) | xargs nix-store -r</pre>
<syntaxhighlight lang="bash">
<pre class="shell">warning: you did not specify `--add-root'; the result might be removed by the garbage collector</pre>
nix-store -qR $(nix-instantiate /etc/nixos/nixos -A system) | xargs nix-store -r
<pre class="shell"></pre>
warning: you did not specify `--add-root'; the result might be removed by the garbage collector
<pre class="shell">&lt;build output and list of successfully realised paths&gt;</pre>
 
<build output and list of successfully realised paths>
</syntaxhighlight>
Repeat for your user and further profiles:
Repeat for your user and further profiles:


<pre class="console">nix-store -qR ~/.nix-profile |xargs nix-store -r</pre>
<syntaxhighlight lang="bash">nix-store -qR ~/.nix-profile |xargs nix-store -r</syntaxhighlight>
The warning can be ignored for profiles that are listed/linked in ''/nix/var/nix/profiles/'' or one of its subdirectories.
The warning can be ignored for profiles that are listed/linked in ''/nix/var/nix/profiles/'' or one of its subdirectories.


Consult man pages of nix-store and nix-instantiate for further information.
Consult man pages of nix-store and nix-instantiate for further information.


== Why &lt;hash&gt;-&lt;name&gt; instead of &lt;name&gt;-&lt;hash&gt;? ==
== Why <hash>-<name> instead of <name>-<hash>? ==


For the rare cases where we have to dig into the /nix/store it is more practical to keep in mind the first few letters at the beginning than finding a package by name. In addition, the hash is printed by Nix commands. If you still wonder why, run <code>ls -1 /nix/store | sort -R -t - -k 2 | less</code> in your shell.
For the rare cases where we have to dig into the /nix/store it is more practical to keep in mind the first few letters at the beginning than finding a package by name. In addition, the hash is printed by Nix commands. If you still wonder why, run <code>ls -1 /nix/store | sort -R -t - -k 2 | less</code> in your shell.
Line 57: Line 59:
This is what might happen if you don't garbage collect frequently, or if you are testing compilation variants:
This is what might happen if you don't garbage collect frequently, or if you are testing compilation variants:


<pre class="shell">q0yi2nr8i60gm2zap46ryysydd2nhzhp-automake-1.11.1/
<syntaxhighlight lang="bash">
q0yi2nr8i60gm2zap46ryysydd2nhzhp-automake-1.11.1/
vbi4vwwidvd6kklq2kc0kx3nniwa3acl-automake-1.11.1/
vbi4vwwidvd6kklq2kc0kx3nniwa3acl-automake-1.11.1/
wjgzir57hcbzrq3mcgxiwkyiqss3r4aq-automake-1.11.1/
wjgzir57hcbzrq3mcgxiwkyiqss3r4aq-automake-1.11.1/
Line 65: Line 68:
8jij13smq9kdlqv96hm7y8xmbh2c54iy-nixos-build-vms/
8jij13smq9kdlqv96hm7y8xmbh2c54iy-nixos-build-vms/
j714mv53xi2j4ab4g2i08knqr137fd6l-nixos-build-vms/
j714mv53xi2j4ab4g2i08knqr137fd6l-nixos-build-vms/
xvs7y09jf7j48p6l0p87iypgpq470jqw-nixos-build-vms/</pre>
xvs7y09jf7j48p6l0p87iypgpq470jqw-nixos-build-vms/
</syntaxhighlight>
== I've updated my channel and something is broken, how can I rollback to an earlier channel? ==
== I've updated my channel and something is broken, how can I rollback to an earlier channel? ==


View the available generations of your channel:
View the available generations of your channel:


<pre class="console">nix-env --list-generations -p /nix/var/nix/profiles/per-user/root/channels</pre>
<syntaxhighlight lang="bash">
<pre class="shell">18  2014-04-17 09:16:28
nix-env --list-generations -p /nix/var/nix/profiles/per-user/root/channels
18  2014-04-17 09:16:28
19  2014-06-13 10:31:24  
19  2014-06-13 10:31:24  
20  2014-08-12 19:09:20  (current)</pre>
20  2014-08-12 19:09:20  (current)
</syntaxhighlight>
To rollback to the previous generation:
To rollback to the previous generation:


<pre class="console">nix-env --rollback -p /nix/var/nix/profiles/per-user/root/channels</pre>
<syntaxhighlight lang="bash">
<pre class="shell">switching from generation 20 to 19</pre>
nix-env --rollback -p /nix/var/nix/profiles/per-user/root/channels
switching from generation 20 to 19
</syntaxhighlight>
To switch to a particular generation:
To switch to a particular generation:


<pre class="console">nix-env --switch-generation 18 -p /nix/var/nix/profiles/per-user/root/channels</pre>
<syntaxhighlight lang="bash">
<pre class="shell">switching from generation 20 to 18</pre>
nix-env --switch-generation 18 -p /nix/var/nix/profiles/per-user/root/channels
switching from generation 20 to 18
</syntaxhighlight>
== I'm working on a new package, how can I build it without adding it to nixpkgs? ==
== I'm working on a new package, how can I build it without adding it to nixpkgs? ==


<pre class="console">nix-build -E 'with import &lt;nixpkgs&gt; { }; callPackage ./mypackage.nix { }'</pre>
<syntaxhighlight lang="bash">nix-build -E 'with import <nixpkgs> { }; callPackage ./mypackage.nix { }'</syntaxhighlight>
You can replace callPackage with callPackage_i686 to build the 32-bit version of your package on a 64-bit system if you want to test that.
You can replace callPackage with callPackage_i686 to build the 32-bit version of your package on a 64-bit system if you want to test that.


Line 91: Line 101:
To build a package with -O0 and -g, and without stripping debug symbols use:
To build a package with -O0 and -g, and without stripping debug symbols use:


<pre class="console">nix-build -E 'with import &lt;nixpkgs&gt; { }; enableDebugging fooPackage'</pre>
<syntaxhighlight lang="bash">nix-build -E 'with import <nixpkgs> { }; enableDebugging fooPackage'</syntaxhighlight>
== How can I force a rebuild from source even without modifying the nix expression? ==
== How can I force a rebuild from source even without modifying the nix expression? ==


As root you can run nix-build with the --check flag:
As root you can run nix-build with the --check flag:


<pre class="console">sudo nix-build --check -A ncdu</pre>
<syntaxhighlight lang="bash">sudo nix-build --check -A ncdu</syntaxhighlight>
== How can I manage software with nix-env like with configuration.nix? ==
== How can I manage software with nix-env like with configuration.nix? ==


Line 103: Line 113:
<ol style="list-style-type: decimal;">
<ol style="list-style-type: decimal;">
<li><p>Create a meta package called ''userPackages'' your ''~/.config/nixpkgs/config.nix'' file with the packages you would like to have in your environment:</p>
<li><p>Create a meta package called ''userPackages'' your ''~/.config/nixpkgs/config.nix'' file with the packages you would like to have in your environment:</p>
<pre class="nix"> with (import &lt;nixpkgs&gt; {});
<syntaxhighlight lang="nix">
  {
with (import <nixpkgs> {});
packageOverrides = pkgs: with pkgs; {
{
userPackages = buildEnv {
  packageOverrides = pkgs: with pkgs; {
  inherit ((import &lt;nixpkgs/nixos&gt; {}).config.system.path)
  userPackages = buildEnv {
    inherit ((import <nixpkgs/nixos> {}).config.system.path)
     pathsToLink ignoreCollisions postBuild;
     pathsToLink ignoreCollisions postBuild;
  extraOutputsToInstall = [ &quot;man&quot; ];
    extraOutputsToInstall = [ "man" ];
  name = &quot;user-packages&quot;;
    name = "user-packages";
  paths = [ vim git wget ];
    paths = [ vim git wget ];
};
  };
  }</pre></li>
}
</syntaxhighlight></li>
<li><p>Install all specified packages using this command:</p>
<li><p>Install all specified packages using this command:</p>
<pre class="console">nix-env -iA userPackages -f '&lt;nixpkgs&gt;'</pre></li></ol>
<syntaxhighlight lang="bash">nix-env -iA userPackages -f '<nixpkgs>'</syntaxhighlight></li></ol>


Now you can add and remove packages from the paths list and rerun nix-env to update your user local packages.
Now you can add and remove packages from the paths list and rerun nix-env to update your user local packages.
Line 123: Line 135:
It probably just needs to know where to find the libraries it needs. You can use [https://nixos.org/patchelf.html patchelf] to set the library path and dynamic linker appropriately:
It probably just needs to know where to find the libraries it needs. You can use [https://nixos.org/patchelf.html patchelf] to set the library path and dynamic linker appropriately:


<pre class="nix"># mybinaryprogram.nix
<syntaxhighlight lang="nix">
with import &lt;nixpkgs&gt; {}; with xlibs;
# mybinaryprogram.nix
with import <nixpkgs> {}; with xlibs;
stdenv.mkDerivation rec {
stdenv.mkDerivation rec {
   name = &quot;somename&quot;;
   name = "somename";
   buildInputs = [ makeWrapper ];
   buildInputs = [ makeWrapper ];
   buildPhase = &quot;true&quot;;
   buildPhase = "true";
   libPath = lib.makeLibraryPath [ libXrandr libXinerama libXcursor ];
   libPath = lib.makeLibraryPath [ libXrandr libXinerama libXcursor ];
   unpackPhase = &quot;true&quot;;
   unpackPhase = "true";
   installPhase = ''
   installPhase = ''
     mkdir -p $out/bin
     mkdir -p $out/bin
     cp ${./mybinaryprogram} $out/bin/mybinaryprogram
     cp ${./mybinaryprogram} $out/bin/mybinaryprogram
     patchelf \
     patchelf \
     --set-interpreter &quot;$(&lt; &quot;$NIX_CC/nix-support/dynamic-linker&quot;)&quot; \
     --set-interpreter "$(< "$NIX_CC/nix-support/dynamic-linker")" \
     --set-rpath &quot;${libPath}&quot; \
     --set-rpath "${libPath}" \
     $out/bin/mybinaryprogram
     $out/bin/mybinaryprogram
   '';
   '';
}</pre>
}</syntaxhighlight>
This can be built with:
This can be built with:


<pre class="console">nix-build mybinaryprogram.nix</pre>
<syntaxhighlight lang="bash">nix-build mybinaryprogram.nix</syntaxhighlight>
And run with:
And run with:


<pre class="console">./result/bin/mybinaryprogram</pre>
<syntaxhighlight lang="bash">./result/bin/mybinaryprogram</syntaxhighlight>
Another possibility is using a FHS-compatible Sandbox with [https://nixos.org/nixpkgs/manual/#sec-fhs-environments buildFHSUserEnv]
Another possibility is using a FHS-compatible Sandbox with [https://nixos.org/nixpkgs/manual/#sec-fhs-environments buildFHSUserEnv]


<pre class="nix"># fhsUser.nix
<syntaxhighlight lang="nix">
{ pkgs ? import &lt;nixpkgs&gt; {} }:
# fhsUser.nix
{ pkgs ? import <nixpkgs> {} }:
(pkgs.buildFHSUserEnv {
(pkgs.buildFHSUserEnv {
   name = &quot;example-env&quot;;
   name = "example-env";
   targetPkgs = pkgs: with pkgs; [
   targetPkgs = pkgs: with pkgs; [
     coreutils
     coreutils
Line 166: Line 180:
     mesa_glu
     mesa_glu
   ];
   ];
   runScript = &quot;bash&quot;;
   runScript = "bash";
}).env</pre>
}).env</syntaxhighlight>
the sandbox can be entered with
the sandbox can be entered with


<pre class="console">nix-shell fhsUser.nix</pre>
<syntaxhighlight lang="bash">nix-shell fhsUser.nix</syntaxhighlight>
== What are channels and how they get updated? ==
== What are channels and how they get updated? ==


[https://github.com/NixOS/nixpkgs Nixpkgs] is the git repository containing all packages and NixOS modules/expressions. Installing packages directly from Nixpkgs master branch is possible but a bit risky as git commits are merged into master before being heavily tested. That's where channels are useful.
[https://github.com/NixOS/nixpkgs Nixpkgs] is the git repository containing all packages and NixOS modules/expressions. Installing packages directly from Nixpkgs master branch is possible but a bit risky as git commits are merged into master before being heavily tested. That's where channels are useful.


A &quot;channel&quot; is a name for the latest &quot;verified&quot; git commits in Nixpkgs. Each channel has a different definition of what &quot;verified&quot; means. Each time a new git commit is verified, the channel declaring this verification gets updated. Contrary to an user of the git master branch, a channel user will benefit both from verified commits and binary packages from the binary cache.
A "channel" is a name for the latest "verified" git commits in Nixpkgs. Each channel has a different definition of what "verified" means. Each time a new git commit is verified, the channel declaring this verification gets updated. Contrary to an user of the git master branch, a channel user will benefit both from verified commits and binary packages from the binary cache.


Channels are reified as git branches in the [https://github.com/NixOS/nixpkgs-channels nixpkgs-channels repository] and as disk images in the [https://nixos.org/channels/ channels webpage]. There are several channels, each with its own use case and verification phase:
Channels are reified as git branches in the [https://github.com/NixOS/nixpkgs-channels nixpkgs-channels repository] and as disk images in the [https://nixos.org/channels/ channels webpage]. There are several channels, each with its own use case and verification phase:
Line 211: Line 225:
If you want to know where <nixpkgs> is located:
If you want to know where <nixpkgs> is located:


<pre class="console">nix-instantiate --find-file nixpkgs</pre>
<syntaxhighlight lang="bash">nix-instantiate --find-file nixpkgs</syntaxhighlight>
To know the commit, open the .version-suffix file in the nixpkgs location. The hash after the dot is the git commit.
To know the commit, open the .version-suffix file in the nixpkgs location. The hash after the dot is the git commit.


Line 224: Line 238:
start a new shell with a private mount namespace (Linux-only)
start a new shell with a private mount namespace (Linux-only)


<pre class="console">sudo unshare -m bash</pre>
<syntaxhighlight lang="bash">sudo unshare -m bash</syntaxhighlight>
remount the filesystem with write privileges (as root)
remount the filesystem with write privileges (as root)


<pre class="console">mount -o remount,rw /nix/store</pre>
<syntaxhighlight lang="bash">mount -o remount,rw /nix/store</syntaxhighlight>
update the file
update the file


<pre class="console">nano &lt;PATH_TO_PACKAGE&gt;/default.nix</pre>
<syntaxhighlight lang="bash">nano <PATH_TO_PACKAGE>/default.nix</syntaxhighlight>
exit to shell where /nix/store is still mounted read-only
exit to shell where /nix/store is still mounted read-only


<pre class="console">exit</pre>
<syntaxhighlight lang="bash">exit</syntaxhighlight>
Be sure to [https://github.com/NixOS/nixpkgs/issues report the incorrect url] or [https://github.com/NixOS/nixpkgs/pulls fix it yourself].
Be sure to [https://github.com/NixOS/nixpkgs/issues report the incorrect url] or [https://github.com/NixOS/nixpkgs/pulls fix it yourself].


Line 242: Line 256:
For instance to get the checksum of a git repository use:
For instance to get the checksum of a git repository use:


<pre class="console">nix-prefetch-git https://git.zx2c4.com/password-store</pre>
<syntaxhighlight lang="bash">nix-prefetch-git https://git.zx2c4.com/password-store</syntaxhighlight>
== Should I use http://hydra.nixos.org/ as a binary cache? ==
== Should I use http://hydra.nixos.org/ as a binary cache? ==


Line 257: Line 271:
== I'm trying to install NixOS but my Wifi isn't working and I don't have an ethernet port ==
== I'm trying to install NixOS but my Wifi isn't working and I don't have an ethernet port ==


Most phones will allow you to share your Wifi connection over USB. On Android you can enable this setting via ''Settings'' &gt; ''Wireless &amp; Networks'' / More ... &gt; ''Tethering &amp; portable hotspot'' &gt; ''USB tethering''. This should be enough to allow you to install NixOS, and then fix your Wifi. iPhones only let you tether using your data connection rather than WiFi.
Most phones will allow you to share your Wifi connection over USB. On Android you can enable this setting via ''Settings'' > ''Wireless &amp; Networks'' / More ... > ''Tethering &amp; portable hotspot'' > ''USB tethering''. This should be enough to allow you to install NixOS, and then fix your Wifi. iPhones only let you tether using your data connection rather than WiFi.


== How can I disable the binary cache and build everything locally? ==
== How can I disable the binary cache and build everything locally? ==


Set the binary caches to an empty list: <code>nix.binaryCaches = [];</code> in _configuration.nix or pass ad-hoc <code>--option binary-caches &quot;&quot;</code> as parameter to nix-build or its wrappers.
Set the binary caches to an empty list: <code>nix.binaryCaches = [];</code> in _configuration.nix or pass ad-hoc <code>--option binary-caches ""</code> as parameter to nix-build or its wrappers.


This is also useful to make simple configuration changes in NixOS (ex.: network related), when no network connectivity is available:
This is also useful to make simple configuration changes in NixOS (ex.: network related), when no network connectivity is available:


<pre class="console">nixos-rebuild switch --option binary-caches &quot;&quot;</pre>
<syntaxhighlight lang="bash">nixos-rebuild switch --option binary-caches ""</syntaxhighlight>
== How do I enable chrooted builds on non-NixOS? ==
== How do I enable chrooted builds on non-NixOS? ==


Two options have to be added to make chrooted builds work on Nix, ''build-use-chroot'' and ''build-chroot-dirs'':
Two options have to be added to make chrooted builds work on Nix, ''build-use-chroot'' and ''build-chroot-dirs'':


<pre class="nix"># /etc/nix/nix.conf
<syntaxhighlight lang="nix">
# /etc/nix/nix.conf
build-use-chroot = true
build-use-chroot = true
build-chroot-dirs = $(nix-store -qR $(nix-build '&lt;nixpkgs&gt;' -A bash) | xargs echo /bin/sh=$(nix-build '&lt;nixpkgs&gt;' -A bash)/bin/bash)</pre>
build-chroot-dirs = $(nix-store -qR $(nix-build '<nixpkgs>' -A bash) | xargs echo /bin/sh=$(nix-build '<nixpkgs>' -A bash)/bin/bash)
</syntaxhighlight>
On NixOS set the following in ''configuration.nix'':
On NixOS set the following in ''configuration.nix'':


<pre class="nix">nix.extraOptions = ''
<syntaxhighlight lang="nix">
nix.extraOptions = ''
   build-use-sandbox = true
   build-use-sandbox = true
'';</pre>
'';
</syntaxhighlight>
== I cannot find $package when running <code>nix-env -qaP</code> even with channels configured ==
== I cannot find $package when running <code>nix-env -qaP</code> even with channels configured ==


Line 286: Line 304:
If you want to install an unfree package as a user, then you need to enable it in ''~/.nixpkgs/config.nix'':
If you want to install an unfree package as a user, then you need to enable it in ''~/.nixpkgs/config.nix'':


<pre class="nix">{
<syntaxhighlight lang="nix">
{
   ...
   ...
   allowUnfree = true;
   allowUnfree = true;
}</pre>
}
</syntaxhighlight>
If you want to enable unfree packages system-wide, then set in your <code>/etc/nixos/configuration.nix</code>:
If you want to enable unfree packages system-wide, then set in your <code>/etc/nixos/configuration.nix</code>:


<pre class="nix">{
<syntaxhighlight lang="nix">
{
   ...
   ...
   nixpkgs.config.allowUnfree = true;
   nixpkgs.config.allowUnfree = true;
}</pre>
}
</syntaxhighlight>
For temporary allowing unfree packages you can set the environment variable ''NIXPKGS_ALLOW_UNFREE'', e.g.
For temporary allowing unfree packages you can set the environment variable ''NIXPKGS_ALLOW_UNFREE'', e.g.


<pre class="console">NIXPKGS_ALLOW_UNFREE=1 nix-env</pre>
<syntaxhighlight lang="bash">NIXPKGS_ALLOW_UNFREE=1 nix-env</syntaxhighlight>
== How can I install a package from unstable while remaining on the stable channel? ==
== How can I install a package from unstable while remaining on the stable channel? ==


It is possible to have multiple nix-channels simultaneously. To add the unstable channel with the specifier ''unstable'',
It is possible to have multiple nix-channels simultaneously. To add the unstable channel with the specifier ''unstable'',


<pre class="console">sudo nix-channel --add https://nixos.org/channels/nixos-unstable unstable</pre>
<syntaxhighlight lang="bash">sudo nix-channel --add https://nixos.org/channels/nixos-unstable unstable</syntaxhighlight>
After updating the channel
After updating the channel


<pre class="console">sudo nix-channel --update unstable</pre>
<syntaxhighlight lang="bash">sudo nix-channel --update unstable</syntaxhighlight>
queries via <code>nix-env</code> (or <code>nox</code>) will show packages from both ''stable'' and ''unstable''. Use this to install unstable packages into your user environment. The following snippet shows how this can be done in ''configuration.nix''.
queries via <code>nix-env</code> (or <code>nox</code>) will show packages from both ''stable'' and ''unstable''. Use this to install unstable packages into your user environment. The following snippet shows how this can be done in ''configuration.nix''.


<pre class="nix">{ config, pkgs, ... }:
<syntaxhighlight lang="nix">
{ config, pkgs, ... }:
let
let
   unstable = import &lt;nixos-unstable&gt; {};
   unstable = import <nixos-unstable> {};
in {
in {
   environment.systemPackages = [ unstable.PACKAGE_NAME ];
   environment.systemPackages = [ unstable.PACKAGE_NAME ];
}</pre>
}
</syntaxhighlight>
= What is the origin of the name <code>Nix</code> =
= What is the origin of the name <code>Nix</code> =


<blockquote>The name <code>Nix</code> is derived from the Dutch word ''niks'', meaning ''nothing'';build actions do not see anything that has not been explicitly declared as an input &gt; [https://pdfs.semanticscholar.org/5fd8/8f89bd8738816e62808a1b7fb12d3ab14a2f.pdf Nix: A Safe and Policy-Free System for Software Deployment, page 2]
<blockquote>The name <code>Nix</code> is derived from the Dutch word ''niks'', meaning ''nothing'';build actions do not see anything that has not been explicitly declared as an input > [https://pdfs.semanticscholar.org/5fd8/8f89bd8738816e62808a1b7fb12d3ab14a2f.pdf Nix: A Safe and Policy-Free System for Software Deployment, page 2]
</blockquote>
</blockquote>