FAQ: Difference between revisions

imported>Simonmichael
Klinger (talk | contribs)
m changed link from redirect to article
 
(36 intermediate revisions by 21 users not shown)
Line 2: Line 2:


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? ==
The old wiki at nixos.wiki has several problems:
* Many components (mediawiki, php, icu) are severely outdated.
** MediaWiki 1.29 (EOL 2018), now 1.35 (EOL 2023-12)
** PHP 7.3.33 (EOL 2021-12)
** ICU 64.2
* Cloudflare DDOS protection makes wiki edits fail sometimes.
* There is no WYSIWYG editor.
* The wiki infrastructure, which was supposed to be made public after launch, never ended-up being made public.
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.
Also see:
* https://nixos.wiki/wiki/User:Winny/WikiRisks
* 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? ==


Mainly because Nix is intended to be lightweight, easy to learn and portable (zero dependencies). Since 24. April 2017 thanks to [https://github.com/shlevy Shea Levy] and the [https://www.gofundme.com/htuafwrg/ crowdfunding of 54 community members], nix does not have Perl as dependency anymore.
Mainly because Nix is intended to be lightweight, easy to learn, and portable (zero dependencies).
{{:FAQ/Libraries}}
 
{{:FAQ/nix-env -iA}}
{{:FAQ/stateVersion}}
== 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? ==


Line 14: Line 30:
{ config, pkgs, lib, ... }:
{ config, pkgs, lib, ... }:
{
{
   nix.extraOptions = ''
   nix.settings = {
     keep-outputs = true
     keep-outputs = true;
     keep-derivations = true
     keep-derivations = true;
   '';
    # See https://nixos.org/manual/nix/stable/command-ref/conf-file.html
    # for a complete list of Nix configuration options.
   };
}</syntaxhighlight>
}</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:
Line 25: Line 43:


<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


Line 32: Line 50:
Repeat for your user and further profiles:
Repeat for your user and further profiles:


<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.


Line 43: Line 61:
(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.)


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.


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


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:
Line 111: Line 129:
{
{
   packageOverrides = pkgs: with pkgs; {
   packageOverrides = pkgs: with pkgs; {
  userPackages = buildEnv {
    userPackages = buildEnv {
    inherit ((import <nixpkgs/nixos> {}).config.system.path)
      inherit ((import <nixpkgs/nixos> {}).config.system.path)
    pathsToLink ignoreCollisions postBuild;
      pathsToLink ignoreCollisions postBuild;
    extraOutputsToInstall = [ "man" ];
      extraOutputsToInstall = [ "man" ];
    name = "user-packages";
      name = "user-packages";
    paths = [ vim git wget ];
      paths = [ vim git wget ];
    };
   };
   };
}
}
Line 124: Line 143:


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.
Another way is using [[Home Manager]].


== 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? ==
Line 131: Line 152:


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 />
Compiled binaries have hard-coded interpreter and require certain dynamic libraries. You can use [https://nixos.org/patchelf.html patchelf] to set the library path and dynamic linker appropriately:
 
You can use [https://github.com/Mic92/nix-ld nix-ld] to run compiled binaries. For example, if your binary needs zlib and openssl:
 
<syntaxhighlight lang="nix">
programs.nix-ld = {
  enable = true;
  libraries = [ pkgs.zlib pkgs.openssl ];
};</syntaxhighlight>
 
Log out and back in to apply the environment variables it sets, and you can then directly run the binary.
 
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:
 
<syntaxhighlight lang="nix">
programs.nix-ld = {
  enable = true;
  libraries = pkgs.steam-run.fhsenv.args.multiPkgs pkgs;
};</syntaxhighlight>
 
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.
 
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.


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
Line 188: Line 230:
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 they get updated? ==
== What are channels and how do they get updated? ==
{{main|Nix Channels}}
{{main|Channel branches}}


[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.
Line 195: Line 237:
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.


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 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:


* '''nixos-unstable'''
* '''nixos-unstable'''
Line 207: Line 249:
** '''definition''' this channel is updated depending on [https://github.com/NixOS/nixpkgs/blob/master/pkgs/top-level/release.nix release.nix] and [https://github.com/NixOS/nixpkgs/blob/master/pkgs/top-level/release-lib.nix release-lib.nix]
** '''definition''' this channel is updated depending on [https://github.com/NixOS/nixpkgs/blob/master/pkgs/top-level/release.nix release.nix] and [https://github.com/NixOS/nixpkgs/blob/master/pkgs/top-level/release-lib.nix release-lib.nix]
* '''nixos-YY.MM-small''' (where '''YY''' is a 2-digit year and '''MM''' is a 2-digit month, such as [https://nixos.org/channels/nixos-15.09-small/ nixos-15.09-small])
* '''nixos-YY.MM-small''' (where '''YY''' is a 2-digit year and '''MM''' is a 2-digit month, such as [https://nixos.org/channels/nixos-15.09-small/ nixos-15.09-small])
** '''description''' The difference between <code>nixos-YY.MM-small</code> and <code>nixos-YY.MM</code> is the name 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)


Channel update works as follows:
Channel update works as follows:
Line 231: Line 273:
<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.
== Nixpkgs branches ==
Branches on the nixpkgs repo have a relationship with channels, but that relationship is not 1:1.
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:
* 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
* [[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.
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? ==
Line 275: Line 327:


<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>
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? ==


Line 281: Line 336:
== 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 ==


First of all, NixOS cannot be installed without an internet connection. However, 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.
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.
 
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]]
For connecting to your wifi, see [[NixOS_Installation_Guide#Wireless]]
Line 287: Line 344:
== 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 ""</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.


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:


<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? ==


Line 304: Line 362:


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
nix.useSandbox = true;
nix.settings.sandbox = true;
</syntaxhighlight>
</syntaxhighlight>


See [[Nix Package Manager#Sandbox_builds]] for more details.
See [[Nix package manager#Sandbox_builds]] for more details.


{{:FAQ/notfound}}
== How can I install a package from unstable while remaining on the stable channel? ==
{{:FAQ/unfree}}


== How can I install a package from unstable while remaining on the stable channel? ==
If you simply want to run a ''nix-shell'' with a package from unstable, you can run a command like the following:


''Note: This may temporarily not work because of [https://github.com/NixOS/nix/issues/1865 nix#1865]''
<syntaxhighlight lang="bash">nix-shell -I nixpkgs=channel:nixpkgs-unstable -p somepackage</syntaxhighlight>


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'',
Line 322: Line 379:


<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> (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> 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''.


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
Line 332: Line 389:
}
}
</syntaxhighlight>
</syntaxhighlight>
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">
{ config, pkgs, ... }:
let
  unstable = import <nixos-unstable> {};
in {
  nixpkgs.overlays = [
    (self: super: {
      PACKAGE_NAME = unstable.PACKAGE_NAME;
    })
  ];
}
</syntaxhighlight>
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]).


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:
Line 339: Line 411:


== 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 ==
'''Note:''' If you're using a kernel with at least version 5.6, you don't need to explicitly add this.


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.  
Line 351: Line 425:
<syntaxhighlight lang="bash">nixos-rebuild switch</syntaxhighlight>
<syntaxhighlight lang="bash">nixos-rebuild switch</syntaxhighlight>


Restart NixOS
Restart NixOS.


== What is the origin of the name "Nix" ==


== What is the origin of the name <code>Nix</code> ==
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>


<blockquote>The name <code>Nix</code> is derived from the Dutch word [https://en.wiktionary.org/wiki/nix 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]
== What does it mean to say that NixOS is "immutable" ==
</blockquote>
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.


<hr />
{{:FAQ/Libraries}}
{{:FAQ/nix-env -iA}}
{{:FAQ/stateVersion}}
{{:FAQ/notfound}}
{{:FAQ/unfree}}
<!-- Transclude subpages -->


== References ==
== References ==
[[Category:Cookbook]]