FAQ: Difference between revisions

Klinger (talk | contribs)
m fix the broken wiki link
 
(9 intermediate revisions by 6 users not shown)
Line 1: Line 1:
<languages/>
<translate>
<!--T:1-->
Frequently asked questions and common newcomer trouble should be put here so that we can point to this page instead of answering the same question over and over again.
Frequently asked questions and common newcomer trouble should be put here so that we can point to this page instead of answering the same question over and over again.


<!--T:24-->
http://unix.stackexchange.com/questions/tagged/nixos can also be used for questions.
http://unix.stackexchange.com/questions/tagged/nixos can also be used for questions.


== Why is there a new wiki? What is with nixos.wiki? ==
== Why is there a new wiki? What is with nixos.wiki? == <!--T:25-->


<!--T:26-->
The old wiki at nixos.wiki has several problems:
The old wiki at nixos.wiki has several problems:


<!--T:27-->
* Many components (mediawiki, php, icu) are severely outdated.
* Many components (mediawiki, php, icu) are severely outdated.
** MediaWiki 1.29 (EOL 2018), now 1.35 (EOL 2023-12)
** MediaWiki 1.29 (EOL 2018), now 1.35 (EOL 2023-12)
Line 15: Line 21:
* The wiki infrastructure, which was supposed to be made public after launch, never ended-up being made public.
* The wiki infrastructure, which was supposed to be made public after launch, never ended-up being made public.


<!--T:28-->
We tried to address these issues multiple times over multiple years across multiple channels (email, matrix). We never got a direct answer. The last point of contact was made through zimbatm representing the NixOS foundation, asking the maintainer about possible cooperation on a new wiki. The answer was no. With the old wiki deteriorating and the maintainer unresponsive, forking the content into a new wiki remained the only way forward.
We tried to address these issues multiple times over multiple years across multiple channels (email, matrix). We never got a direct answer. The last point of contact was made through zimbatm representing the NixOS foundation, asking the maintainer about possible cooperation on a new wiki. The answer was no. With the old wiki deteriorating and the maintainer unresponsive, forking the content into a new wiki remained the only way forward.


<!--T:29-->
Also see:
Also see:
* https://nixos.wiki/wiki/User:Winny/WikiRisks
* https://wiki.nixos.org/wiki/User:Winny/WikiRisks
* https://greasyfork.org/en/scripts/495011-redirect-to-wiki-nixos-org (trivial userscript to redirect nixos.wiki links here)
* https://greasyfork.org/en/scripts/495011-redirect-to-wiki-nixos-org (trivial userscript to redirect nixos.wiki links here)


== Why is Nix written in C++ rather than a functional language like Haskell? ==
== Why is Nix written in C++ rather than a functional language like Haskell? == <!--T:2-->


<!--T:30-->
Mainly because Nix is intended to be lightweight, easy to learn, and portable (zero dependencies).
Mainly because Nix is intended to be lightweight, easy to learn, and portable (zero dependencies).


== 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? == <!--T:3-->


<!--T:31-->
<syntaxhighlight lang="nix"># /etc/nixos/configuration.nix
<syntaxhighlight lang="nix"># /etc/nixos/configuration.nix
{ config, pkgs, lib, ... }:
{ config, pkgs, lib, ... }:
Line 39: Line 49:
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:


<!--T:32-->
<syntaxhighlight lang="bash">nixos-rebuild switch</syntaxhighlight>
<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:


<!--T:33-->
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
nix-store -qR $(nix-instantiate '<nixpkgs/nixos>' -A system) | xargs nix-store -r
nix-store -qR $(nix-instantiate '<nixpkgs/nixos>' -A system) | xargs nix-store -r
warning: you did not specify `--add-root'; the result might be removed by the garbage collector
warning: you did not specify `--add-root'; the result might be removed by the garbage collector


<!--T:34-->
<build output and list of successfully realised paths>
<build output and list of successfully realised paths>
</syntaxhighlight>
</syntaxhighlight>
Repeat for your user and further profiles:
Repeat for your user and further profiles:


<!--T:35-->
<syntaxhighlight lang="bash">nix-store -qR ~/.nix-profile | xargs nix-store -r</syntaxhighlight>
<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.


<!--T:36-->
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 <hash>-<name> instead of <name>-<hash>? ==
== Why <hash>-<name> instead of <name>-<hash>? == <!--T:4-->


<!--T:37-->
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.  
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.  
Ie, you can uniquely identify almost any storepath with just the first 4-5 characters of the hash.
Ie, you can uniquely identify almost any storepath with just the first 4-5 characters of the hash.
(Rather than having to type out the full package name, then 4-5 characters of the hash.)
(Rather than having to type out the full package name, then 4-5 characters of the hash.)


<!--T:38-->
Also, since the initial part is all of the same length, visually parsing a list of packages is easier.
Also, since the initial part is all of the same length, visually parsing a list of packages is easier.


<!--T:39-->
If you still wonder why, run <code>ls -1 /nix/store | sort -R -t - -k 2 | less</code> in your shell. ''(? unclear)''
If you still wonder why, run <code>ls -1 /nix/store | sort -R -t - -k 2 | less</code> in your shell. ''(? unclear)''


<!--T:40-->
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:


<!--T:41-->
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
q0yi2nr8i60gm2zap46ryysydd2nhzhp-automake-1.11.1/
q0yi2nr8i60gm2zap46ryysydd2nhzhp-automake-1.11.1/
Line 79: Line 99:
</syntaxhighlight>
</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? == <!--T:5-->


<!--T:42-->
View the available generations of your channel:
View the available generations of your channel:


<!--T:43-->
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
nix-env --list-generations -p /nix/var/nix/profiles/per-user/root/channels
nix-env --list-generations -p /nix/var/nix/profiles/per-user/root/channels
Line 91: Line 113:
To rollback to the previous generation:
To rollback to the previous generation:


<!--T:44-->
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
nix-env --rollback -p /nix/var/nix/profiles/per-user/root/channels
nix-env --rollback -p /nix/var/nix/profiles/per-user/root/channels
Line 97: Line 120:
To switch to a particular generation:
To switch to a particular generation:


<!--T:45-->
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
nix-env --switch-generation 18 -p /nix/var/nix/profiles/per-user/root/channels
nix-env --switch-generation 18 -p /nix/var/nix/profiles/per-user/root/channels
switching from generation 20 to 18
switching from generation 20 to 18
</syntaxhighlight>
</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? == <!--T:6-->
<!--T:46-->
<syntaxhighlight lang="bash">nix-build -E 'with import <nixpkgs> { }; callPackage ./mypackage.nix { }'</syntaxhighlight>
<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.


== How can I compile a package with debugging symbols included? ==
== How can I compile a package with debugging symbols included? == <!--T:7-->


<!--T:47-->
To build a package with -Og and -g, and without stripping debug symbols use:
To build a package with -Og and -g, and without stripping debug symbols use:


<!--T:48-->
<syntaxhighlight lang="bash">nix-build -E 'with import <nixpkgs> { }; enableDebugging fooPackage'</syntaxhighlight>
<syntaxhighlight lang="bash">nix-build -E 'with import <nixpkgs> { }; enableDebugging fooPackage'</syntaxhighlight>


<!--T:49-->
See also [[Debug Symbols]]
See also [[Debug Symbols]]


== 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? == <!--T:8-->


<!--T:50-->
As root you can run nix-build with the --check flag:
As root you can run nix-build with the --check flag:


<!--T:51-->
<syntaxhighlight lang="bash">sudo nix-build --check -A ncdu</syntaxhighlight>
<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? ==


<!--T:52-->
There are many ways, one is the following:
There are many ways, one is the following:


<!--T:53-->
<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>
Line 142: Line 175:
<syntaxhighlight lang="bash">nix-env -iA userPackages -f '<nixpkgs>'</syntaxhighlight></li></ol>
<syntaxhighlight lang="bash">nix-env -iA userPackages -f '<nixpkgs>'</syntaxhighlight></li></ol>


<!--T:54-->
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.


<!--T:55-->
Another way is using [[Home Manager]].
Another way is using [[Home Manager]].


<!--T:9-->
== I've downloaded a binary, but I can't run it, what can I do? ==
== I've downloaded a binary, but I can't run it, what can I do? ==
Binaries normally do not work out of the box when you download them because they normally just assume that libraries can be found in hardcoded paths such as <code>/lib</code>. However this assumption is incorrect on NixOS systems due to the inner workings of <code>nix</code> - there is no default path, everything gets set to the corresponding version on compile time.
Binaries normally do not work out of the box when you download them because they normally just assume that libraries can be found in hardcoded paths such as <code>/lib</code>. However this assumption is incorrect on NixOS systems due to the inner workings of <code>nix</code> - there is no default path, everything gets set to the corresponding version on compile time.


<!--T:56-->
If you are new to packaging proprietary software you should check out the [[Packaging Binaries|Packaging Binaries Tutorial]].
If you are new to packaging proprietary software you should check out the [[Packaging Binaries|Packaging Binaries Tutorial]].


<!--T:57-->
If you are in a hurry and just want to get shit running, continue reading:<br />
If you are in a hurry and just want to get shit running, continue reading:<br />


<!--T:58-->
You can use [https://github.com/Mic92/nix-ld nix-ld] to run compiled binaries. For example, if your binary needs zlib and openssl:
You can use [https://github.com/Mic92/nix-ld nix-ld] to run compiled binaries. For example, if your binary needs zlib and openssl:


<!--T:59-->
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
programs.nix-ld = {
programs.nix-ld = {
Line 161: Line 201:
};</syntaxhighlight>
};</syntaxhighlight>


<!--T:60-->
Log out and back in to apply the environment variables it sets, and you can then directly run the binary.
Log out and back in to apply the environment variables it sets, and you can then directly run the binary.


<!--T:61-->
If you don't want to configure the list of libraries manually, a quick and dirty way to run nearly any precompiled binary is the following:
If you don't want to configure the list of libraries manually, a quick and dirty way to run nearly any precompiled binary is the following:


<!--T:62-->
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
programs.nix-ld = {
programs.nix-ld = {
Line 171: Line 214:
};</syntaxhighlight>
};</syntaxhighlight>


<!--T:63-->
This uses the libraries that are used by [[Steam]] to simulate a traditional Linux FHS environment to run games in. It's a [https://github.com/NixOS/nixpkgs/blob/nixos-unstable/pkgs/games/steam/fhsenv.nix big list] that usually contains all the libraries your binary needs to run.
This uses the libraries that are used by [[Steam]] to simulate a traditional Linux FHS environment to run games in. It's a [https://github.com/NixOS/nixpkgs/blob/nixos-unstable/pkgs/games/steam/fhsenv.nix big list] that usually contains all the libraries your binary needs to run.


<!--T:64-->
Another possibility is to use [https://nixos.org/patchelf.html patchelf] to set the library path and dynamic linker appropriately, since compiled binaries have hard-coded interpreter and require certain dynamic libraries.
Another possibility is to use [https://nixos.org/patchelf.html patchelf] to set the library path and dynamic linker appropriately, since compiled binaries have hard-coded interpreter and require certain dynamic libraries.


<!--T:65-->
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
# mybinaryprogram.nix
# mybinaryprogram.nix
Line 197: Line 243:
This can be built with:
This can be built with:


<!--T:66-->
<syntaxhighlight lang="bash">nix-build mybinaryprogram.nix</syntaxhighlight>
<syntaxhighlight lang="bash">nix-build mybinaryprogram.nix</syntaxhighlight>
And run with:
And run with:


<!--T:67-->
<syntaxhighlight lang="bash">./result/bin/mybinaryprogram</syntaxhighlight>
<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]


<!--T:68-->
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
# fhsUser.nix
# fhsUser.nix
Line 226: Line 275:
the sandbox can be entered with
the sandbox can be entered with


<!--T:10-->
<syntaxhighlight lang="bash">nix-shell fhsUser.nix</syntaxhighlight>
<syntaxhighlight lang="bash">nix-shell fhsUser.nix</syntaxhighlight>
<br>
<br>
If your target application can't find shared libraries inside buildFHSUserEnv, you may run [https://github.com/lexleogryfon/de-generate nix-de-generate] for target application inside FHS, which will generate newenv.nix file, an nix-expression of buildFHSUserEnv with resolved dependencies for shared libraries.  
If your target application can't find shared libraries inside buildFHSUserEnv, you may run [https://github.com/lexleogryfon/de-generate nix-de-generate] for target application inside FHS, which will generate newenv.nix file, an nix-expression of buildFHSUserEnv with resolved dependencies for shared libraries.
 
== What are channels and how do they get updated? ==
== What are channels and how do they get updated? ==
{{main|Channel branches}}
{{main|Channel branches}}


<!--T:69-->
[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.


<!--T:70-->
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.
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.


<!--T:71-->
Channels are reified as git branches in the [https://github.com/NixOS/nixpkgs nixpkgs 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 nixpkgs 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:


<!--T:72-->
* '''nixos-unstable'''
* '''nixos-unstable'''
** '''description''' Use this when you want the latest package and module versions while still benefiting from the binary cache. You can use this channel on non-NixOS systems. This channel corresponds to NixOS’s main development branch, and may thus see radical changes between channel updates. This channel is not recommended for production systems.
** '''description''' Use this when you want the latest package and module versions while still benefiting from the binary cache. You can use this channel on non-NixOS systems. This channel corresponds to NixOS’s main development branch, and may thus see radical changes between channel updates. This channel is not recommended for production systems.
Line 251: Line 304:
** '''description''' The difference between <code>nixos-YY.MM-small</code> and <code>nixos-YY.MM</code> is the same as the one between <code>nixos-unstable-small</code> and <code>nixos-unstable</code> (see above)
** '''description''' The difference between <code>nixos-YY.MM-small</code> and <code>nixos-YY.MM</code> is the same as the one between <code>nixos-unstable-small</code> and <code>nixos-unstable</code> (see above)


<!--T:73-->
Channel update works as follows:
Channel update works as follows:


<!--T:74-->
# Each channel has a particular job at '''hydra.nixos.org''' which must succeed:
# Each channel has a particular job at '''hydra.nixos.org''' which must succeed:


<!--T:75-->
* For NixOS: the trunk-combined [http://hydra.nixos.org/job/nixos/trunk-combined/tested tested] job, which contains some automated NixOS tests.
* For NixOS: the trunk-combined [http://hydra.nixos.org/job/nixos/trunk-combined/tested tested] job, which contains some automated NixOS tests.
* For nixos-small: the unstable-small [http://hydra.nixos.org/job/nixos/unstable-small/tested tested] job.
* For nixos-small: the unstable-small [http://hydra.nixos.org/job/nixos/unstable-small/tested tested] job.
* For nixpkgs: the trunk [http://hydra.nixos.org/job/nixpkgs/trunk/unstable unstable] job, which contains some critical release packages.
* For nixpkgs: the trunk [http://hydra.nixos.org/job/nixpkgs/trunk/unstable unstable] job, which contains some critical release packages.


<!--T:76-->
<ol start="2" style="list-style-type: decimal;">
<ol start="2" style="list-style-type: decimal;">
<li>Once the job succeeds at a particular nixpkgs commit, '''cache.nixos.org''' will download binaries from '''hydra.nixos.org'''.</li>
<li>Once the job succeeds at a particular nixpkgs commit, '''cache.nixos.org''' will download binaries from '''hydra.nixos.org'''.</li>
<li>Once the above download completes, the channel updates.</li></ol>
<li>Once the above download completes, the channel updates.</li></ol>


<!--T:77-->
You can checkout the nixpkgs git and reset it to a particular commit of a channel. This will not affect your access to the binary cache.
You can checkout the nixpkgs git and reset it to a particular commit of a channel. This will not affect your access to the binary cache.


== How do I know where's nixpkgs channel located and at which commit? ==
== How do I know where's nixpkgs channel located and at which commit? == <!--T:11-->


<!--T:78-->
First <code>echo $NIX_PATH</code> to see where nix looks for the expressions. Note that nix-env uses ''~/.nix-defexpr'' regardless of ''$NIX_PATH''.
First <code>echo $NIX_PATH</code> to see where nix looks for the expressions. Note that nix-env uses ''~/.nix-defexpr'' regardless of ''$NIX_PATH''.


<!--T:79-->
If you want to know where <nixpkgs> is located:
If you want to know where <nixpkgs> is located:


<!--T:80-->
<syntaxhighlight lang="bash">nix-instantiate --find-file nixpkgs</syntaxhighlight>
<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.


<!--T:12-->
== Nixpkgs branches ==
== Nixpkgs branches ==
Branches on the nixpkgs repo have a relationship with [[channels]], but that relationship is not 1:1.
Branches on the nixpkgs repo have a relationship with channels, but that relationship is not 1:1.


<!--T:81-->
Some branches are reified as channels (e.g. the <code>nixos-XX.YY</code> branches, or <code>nix(os|pkgs)-unstable</code>), whereas others are the starting point for those branches (e.g. the <code>master</code> or <code>release-XX.YY</code> branches). For example:
Some branches are reified as channels (e.g. the <code>nixos-XX.YY</code> branches, or <code>nix(os|pkgs)-unstable</code>), whereas others are the starting point for those branches (e.g. the <code>master</code> or <code>release-XX.YY</code> branches). For example:


<!--T:82-->
* When a change in master needs to be backported to the current NixOS release, it is cherry-picked into the current <code>release-XX.YY</code> branch
* When a change in master needs to be backported to the current NixOS release, it is cherry-picked into the current <code>release-XX.YY</code> branch
* [[Nix_channels#Channel_update_process|Hydra]] picks up this change, runs tests, and if those tests pass, updates the corresponding <code>nixos-XX.YY</code> branch, which is then reified as a channel.
* [[Channel branches#Channel_update_process|Hydra]] picks up this change, runs tests, and if those tests pass, updates the corresponding <code>nixos-XX.YY</code> branch, which is then reified as a channel.


<!--T:83-->
So in short, the <code>relase-XX.YY</code> branches have not been run through Hydra yet, whereas the <code>nixos-XX.YY</code> ones have.
So in short, the <code>relase-XX.YY</code> branches have not been run through Hydra yet, whereas the <code>nixos-XX.YY</code> ones have.


== There's an updated version for $software on nixpkgs but not in channels, how can I use it? ==
== There's an updated version for $software on nixpkgs but not in channels, how can I use it? == <!--T:13-->


<!--T:84-->
You can jump the queue and use <code>nix-shell</code> with a <code>NIX_PATH</code> pointing to a tarball of the channel to get a shell for that software. Some building may occur. This will not work for system services.
You can jump the queue and use <code>nix-shell</code> with a <code>NIX_PATH</code> pointing to a tarball of the channel to get a shell for that software. Some building may occur. This will not work for system services.


<!--T:85-->
<syntaxhighlight lang="command">
<syntaxhighlight lang="command">
NIX_PATH=nixpkgs=https://github.com/NixOS/nixpkgs/archive/release-17.09.tar.gz nix-shell -p $software
NIX_PATH=nixpkgs=https://github.com/NixOS/nixpkgs/archive/release-17.09.tar.gz nix-shell -p $software
</syntaxhighlight>
</syntaxhighlight>


== How do I install a specific version of a package for build reproducibility etc.? ==
== How do I install a specific version of a package for build reproducibility etc.? == <!--T:14-->


<!--T:86-->
See [[FAQ/Pinning Nixpkgs]] and [[How to fetch Nixpkgs with an empty NIX PATH]]. Find the version of nixpkgs with the package version you want and pin nixpkgs to that. However, be aware that the pinning of a package of another nixpkgs version results in a much larger package size as not only the package itself but all dependencies (down to libc) have older versions.
See [[FAQ/Pinning Nixpkgs]] and [[How to fetch Nixpkgs with an empty NIX PATH]]. Find the version of nixpkgs with the package version you want and pin nixpkgs to that. However, be aware that the pinning of a package of another nixpkgs version results in a much larger package size as not only the package itself but all dependencies (down to libc) have older versions.


<!--T:87-->
if you just want the old version of the single package but with new dependencies it is often easier to copy the package description into your scope and add it to your <code>configuration.nix</code> via: <code> mypackage-old = pkgs.callPackage ./mypackage-old.nix {};</code>.You can try to build the package as described in [[FAQ#I.27m_working_on_a_new_package.2C_how_can_I_build_it_without_adding_it_to_nixpkgs.3F|the FAQ: building a single derivation]].
if you just want the old version of the single package but with new dependencies it is often easier to copy the package description into your scope and add it to your <code>configuration.nix</code> via: <code> mypackage-old = pkgs.callPackage ./mypackage-old.nix {};</code>.You can try to build the package as described in [[FAQ#I.27m_working_on_a_new_package.2C_how_can_I_build_it_without_adding_it_to_nixpkgs.3F|the FAQ: building a single derivation]].


== An error occurs while fetching sources from an url, how do I fix it? ==
== An error occurs while fetching sources from an url, how do I fix it? == <!--T:15-->


<!--T:88-->
First try to update the local nixpkgs expressions with <code>nix-channel --update</code> (these describe where to download sources from and how to build them). Try your build again and the url might have already been correctly updated for the package in question. You can also subscribe the unstable channel (which includes the most up-to-date expressions) with <code>nix-channel --add http://nixos.org/channels/nixpkgs-unstable</code>, update and try the build again.
First try to update the local nixpkgs expressions with <code>nix-channel --update</code> (these describe where to download sources from and how to build them). Try your build again and the url might have already been correctly updated for the package in question. You can also subscribe the unstable channel (which includes the most up-to-date expressions) with <code>nix-channel --add http://nixos.org/channels/nixpkgs-unstable</code>, update and try the build again.


If that fails you can update the url in the nix expression yourself. [https://nixos.org/wiki/FAQ#How_do_I_know_where.27s_nixpkgs_channel_located_and_at_which_commit.3F Navigate to your channel's expressions] and find the package in one of the subdirectories. Edit the respective ''default.nix'' file by altering the ''url'' and ''sha256''. You can use <code>nix-prefetch-url url</code> to get the SHA-256 hash of source distributions.
<!--T:89-->
If that fails you can update the url in the nix expression yourself. [[#How_do_I_know_where's_nixpkgs_channel_located_and_at_which_commit?|Navigate to your channel's expressions]] and find the package in one of the subdirectories. Edit the respective ''default.nix'' file by altering the ''url'' and ''sha256''. You can use <code>nix-prefetch-url url</code> to get the SHA-256 hash of source distributions.


<!--T:90-->
If the shell complains that you do not have write privileges for the file system, you will have to enable them.
If the shell complains that you do not have write privileges for the file system, you will have to enable them.


<!--T:91-->
start a new shell with a private mount namespace (Linux-only)
start a new shell with a private mount namespace (Linux-only)


<!--T:92-->
<syntaxhighlight lang="bash">sudo unshare -m bash</syntaxhighlight>
<syntaxhighlight lang="bash">sudo unshare -m bash</syntaxhighlight>
remount the filesystem with write privileges (as root)
remount the filesystem with write privileges (as root)


<!--T:93-->
<syntaxhighlight lang="bash">mount -o remount,rw /nix/store</syntaxhighlight>
<syntaxhighlight lang="bash">mount -o remount,rw /nix/store</syntaxhighlight>
update the file
update the file


<!--T:94-->
<syntaxhighlight lang="bash">nano <PATH_TO_PACKAGE>/default.nix</syntaxhighlight>
<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


<!--T:95-->
<syntaxhighlight lang="bash">exit</syntaxhighlight>
<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].


== How do I know the sha256 to use with fetchgit, fetchsvn, fetchbzr or fetchcvs? ==
== How do I know the sha256 to use with fetchgit, fetchsvn, fetchbzr or fetchcvs? == <!--T:16-->


<!--T:96-->
Install <code>nix-prefetch-scripts</code> and use the corresponding nix prefetch helper.
Install <code>nix-prefetch-scripts</code> and use the corresponding nix prefetch helper.


<!--T:97-->
For instance to get the checksum of a git repository use:
For instance to get the checksum of a git repository use:


<!--T:98-->
<syntaxhighlight lang="bash">nix-prefetch-git https://git.zx2c4.com/password-store</syntaxhighlight>
<syntaxhighlight lang="bash">nix-prefetch-git https://git.zx2c4.com/password-store</syntaxhighlight>


<!--T:99-->
Or, use <code>lib.fakeHash</code> as the fetcher's hash argument, and attempt to build; Nix will tell you the actual and expected hash's mismatch, and you may copy the actual hash.
Or, use <code>lib.fakeHash</code> as the fetcher's hash argument, and attempt to build; Nix will tell you the actual and expected hash's mismatch, and you may copy the actual hash.


== Should I use http://hydra.nixos.org/ as a binary cache? ==
== Should I use http://hydra.nixos.org/ as a binary cache? == <!--T:17-->


<!--T:100-->
No. As of 2017, all build artifacts are directly pushed to http://cache.nixos.org/ and are available there, therefore setting http://hydra.nixos.org/ as a binary cache no longer serves any function.
No. As of 2017, all build artifacts are directly pushed to http://cache.nixos.org/ and are available there, therefore setting http://hydra.nixos.org/ as a binary cache no longer serves any function.


== 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 == <!--T:18-->


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.  
<!--T:101-->
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.  


<!--T:102-->
It is also possible to build a custom NixOS installation ISO containing all the dependencies needed for an offline installation, but the default installation ISOs require internet connectivity.
It is also possible to build a custom NixOS installation ISO containing all the dependencies needed for an offline installation, but the default installation ISOs require internet connectivity.


For connecting to your wifi, see [[NixOS_Installation_Guide#Wireless]]
<!--T:103-->
For connecting to your WiFi, see [[NixOS_Installation_Guide#Wireless]]


== How can I disable the binary cache and build everything locally? ==
== How can I disable the binary cache and build everything locally? == <!--T:19-->


<!--T:104-->
Set the binary caches to an empty list: <code>nix.binaryCaches = [];</code> in <code>configuration.nix</code> or pass ad-hoc <code>--option binary-caches <nowiki>''</nowiki></code> as parameter to nix-build or its wrappers.
Set the binary caches to an empty list: <code>nix.binaryCaches = [];</code> in <code>configuration.nix</code> or pass ad-hoc <code>--option binary-caches <nowiki>''</nowiki></code> as parameter to nix-build or its wrappers.


<!--T:105-->
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:


<!--T:106-->
<syntaxhighlight lang="bash">nixos-rebuild switch --option binary-caches ''</syntaxhighlight>
<syntaxhighlight lang="bash">nixos-rebuild switch --option binary-caches ''</syntaxhighlight>


== How do I enable sandboxed builds on non-NixOS? ==
== How do I enable sandboxed builds on non-NixOS? == <!--T:20-->


<!--T:107-->
Two options have to be added to make sandboxed builds work on Nix, ''build-use-sandbox'' and ''build-sandbox-paths'':
Two options have to be added to make sandboxed builds work on Nix, ''build-use-sandbox'' and ''build-sandbox-paths'':


<!--T:108-->
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
# /etc/nix/nix.conf
# /etc/nix/nix.conf
Line 361: Line 451:
On NixOS set the following in ''configuration.nix'':
On NixOS set the following in ''configuration.nix'':


<!--T:109-->
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
nix.settings.sandbox = true;
nix.settings.sandbox = true;
</syntaxhighlight>
</syntaxhighlight>


<!--T:110-->
See [[Nix package manager#Sandbox_builds]] for more details.
See [[Nix package manager#Sandbox_builds]] for more details.


== 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? == <!--T:21-->


<!--T:111-->
If you simply want to run a ''nix-shell'' with a package from unstable, you can run a command like the following:
If you simply want to run a ''nix-shell'' with a package from unstable, you can run a command like the following:


<!--T:112-->
<syntaxhighlight lang="bash">nix-shell -I nixpkgs=channel:nixpkgs-unstable -p somepackage</syntaxhighlight>
<syntaxhighlight lang="bash">nix-shell -I nixpkgs=channel:nixpkgs-unstable -p somepackage</syntaxhighlight>


<!--T:113-->
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'',


<!--T:114-->
<syntaxhighlight lang="bash">sudo nix-channel --add https://nixos.org/channels/nixos-unstable nixos-unstable</syntaxhighlight>
<syntaxhighlight lang="bash">sudo nix-channel --add https://nixos.org/channels/nixos-unstable nixos-unstable</syntaxhighlight>
After updating the channel
After updating the channel


<!--T:115-->
<syntaxhighlight lang="bash">sudo nix-channel --update nixos-unstable</syntaxhighlight>
<syntaxhighlight lang="bash">sudo nix-channel --update nixos-unstable</syntaxhighlight>
queries via <code>nix-env</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> 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''.


<!--T:116-->
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
{ config, pkgs, ... }:
{ config, pkgs, ... }:
Line 390: Line 488:
</syntaxhighlight>
</syntaxhighlight>


<!--T:117-->
This only changes what version of <code>PACKAGE_NAME</code> is available on <code>$PATH</code>. If the package you want to take from unstable is installed through a NixOS module, you must use [[overlays]]:
This only changes what version of <code>PACKAGE_NAME</code> is available on <code>$PATH</code>. If the package you want to take from unstable is installed through a NixOS module, you must use [[overlays]]:
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
Line 405: Line 504:
Note that this will rebuild all packages depending on the overlaid package, which may be a lot. Some modules offer a <code>services.foo.package</code> to change the actual derivation used by the module without and overlay, and without recompiling dependencies ([https://nixos.org/manual/nixos/stable/options.html#opt-services.gvfs.package example]).
Note that this will rebuild all packages depending on the overlaid package, which may be a lot. Some modules offer a <code>services.foo.package</code> to change the actual derivation used by the module without and overlay, and without recompiling dependencies ([https://nixos.org/manual/nixos/stable/options.html#opt-services.gvfs.package example]).


<!--T:118-->
If you want to install unfree packages from unstable you need to also set allowUnfree by replacing the import statment above with:
If you want to install unfree packages from unstable you need to also set allowUnfree by replacing the import statment above with:
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
Line 410: Line 510:
</syntaxhighlight>
</syntaxhighlight>


== I'm unable to connect my USB HDD | External HDD is failing to mount automatically ==
== I'm unable to connect my USB HDD | External HDD is failing to mount automatically == <!--T:22-->


<!--T:119-->
'''Note:''' If you're using a kernel with at least version 5.6, you don't need to explicitly add this.
'''Note:''' If you're using a kernel with at least version 5.6, you don't need to explicitly add this.


<!--T:120-->
exfat is not supported in NixOS by default - since there are legality issues still with exFAT filesystem.  
exfat is not supported in NixOS by default - since there are legality issues still with exFAT filesystem.  


<!--T:121-->
<syntaxhighlight lang="bash">su nano /etc/nixos/configuration.nix</syntaxhighlight>
<syntaxhighlight lang="bash">su nano /etc/nixos/configuration.nix</syntaxhighlight>


<!--T:122-->
Add this line to your configuration file.
Add this line to your configuration file.
<syntaxhighlight lang="bash">boot.extraModulePackages = [ config.boot.kernelPackages.exfat-nofuse ];</syntaxhighlight>
<syntaxhighlight lang="bash">boot.extraModulePackages = [ config.boot.kernelPackages.exfat-nofuse ];</syntaxhighlight>


<!--T:123-->
After saving the file rebuild NixOS:
After saving the file rebuild NixOS:


<!--T:124-->
<syntaxhighlight lang="bash">nixos-rebuild switch</syntaxhighlight>
<syntaxhighlight lang="bash">nixos-rebuild switch</syntaxhighlight>


<!--T:125-->
Restart NixOS.
Restart NixOS.


== What is the origin of the name "Nix" ==
== What is the origin of the name "Nix" == <!--T:23-->


<!--T:126-->
The name <code>Nix</code> comes from the Dutch word [https://en.wiktionary.org/wiki/nix niks] which means ''nothing''. It reflects the fact that Nix derivations do not have access to anything that has not been explicitly declared as an input.<ref>Eelco Dolstra et al. “Nix: A Safe and Policy-Free System for Software Deployment.” LiSA (2004), https://pdfs.semanticscholar.org/5fd8/8f89bd8738816e62808a1b7fb12d3ab14a2f.pdf</ref>
The name <code>Nix</code> comes from the Dutch word [https://en.wiktionary.org/wiki/nix niks] which means ''nothing''. It reflects the fact that Nix derivations do not have access to anything that has not been explicitly declared as an input.<ref>Eelco Dolstra et al. “Nix: A Safe and Policy-Free System for Software Deployment.” LiSA (2004), https://pdfs.semanticscholar.org/5fd8/8f89bd8738816e62808a1b7fb12d3ab14a2f.pdf</ref>


<!--T:127-->
== What does it mean to say that NixOS is "immutable" ==
== What does it mean to say that NixOS is "immutable" ==
Immutability is a property of data, in general, which means that the data cannot be modified after it is created. In the context of an operating system, it really means that certain parts of the system have this property. In the case of Nix and NixOS, that includes the Nix store, where files can be created but not modified after the time they are created. It does not apply to every part of the operating system, in that users can still modify their own files in their home directory, for example.
Immutability is a property of data, in general, which means that the data cannot be modified after it is created. In the context of an operating system, it really means that certain parts of the system have this property. In the case of Nix and NixOS, that includes the Nix store, where files can be created but not modified after the time they are created. It does not apply to every part of the operating system, in that users can still modify their own files in their home directory, for example.


== I'm getting ‘infinite recursion’ errors when trying to do something clever with <code>imports</code> == <!--T:128-->
<!--T:129-->
Evaluating the <code>imports</code> attribute of a NixOS module (such as configuration.nix) is a prerequisite for evaluating just about everything else, so trying anything clever with <code>imports</code> is a common source of infinite recursion (because the evaluator can't determine the values of packages and options without knowing what is imported, and can't determine what is imported without knowing the values of packages or options).
<!--T:130-->
You should not try to conditionally import other modules based on other values. Make your imports unconditional, and make the modules that you're importing have conditional ''behavior'' based on the values of options.
<!--T:131-->
If it helps, think of <code>imports</code> as akin to an <code>#include</code> directive in C.
<!--T:132-->
(Note that none of this applies to the [https://nix.dev/manual/nix/stable/language/builtins#builtins-import <code>import</code> built-in Nix language function], which is its own thing.)
<!--T:133-->
{{:FAQ/Libraries}}
{{:FAQ/Libraries}}
{{:FAQ/nix-env -iA}}
{{:FAQ/nix-env -iA}}
Line 441: Line 565:
<!-- Transclude subpages -->
<!-- Transclude subpages -->


== References ==
== References == <!--T:134-->


<!--T:135-->
[[Category:Cookbook]]
[[Category:Cookbook]]
</translate>