Cheatsheet: Difference between revisions
→Adding files to the store: Fix heading |
m →Using override with nix-build: Highlight shell commands containing nix expression as "console" instead of "nix": we do that in a few other places, so now everything's consistent + otherwise there are too many colors on one line. If you think this should be reverted, you will also need to turn on "nix" highlight in a few other places for consistency |
||
| (13 intermediate revisions by 6 users not shown) | |||
| Line 1: | Line 1: | ||
{{Merge|Nix (command line utilities)}} | |||
This cheatsheet is a collection of handy snippets for working with a NixOS system. | |||
== Working with the nix store == | == Working with the nix store == | ||
| Line 6: | Line 7: | ||
=== Get the store path for a package === | === Get the store path for a package === | ||
< | <syntaxhighlight lang="console"> | ||
$ nix repl | $ nix repl | ||
nix-repl> :l <nixpkgs> | nix-repl> :l <nixpkgs> | ||
| Line 15: | Line 16: | ||
nix-repl> :lf ./configuration.nix # as flakes way for a local file | nix-repl> :lf ./configuration.nix # as flakes way for a local file | ||
# load nixos configuration from a nix file | $ # load nixos configuration from a nix file | ||
$ nix repl --file '<nixpkgs/nixos>' -I nixos-config=./configuration.nix | $ nix repl --file '<nixpkgs/nixos>' -I nixos-config=./configuration.nix | ||
$ nix-build '<nixpkgs>' --no- | $ nix-build '<nixpkgs>' --no-out-link -A xorg.libXtst | ||
/nix/store/nlpnx21yjdjx2ii7ln4kcmbm0x1vy7w9-libXtst-1.2.3 | /nix/store/nlpnx21yjdjx2ii7ln4kcmbm0x1vy7w9-libXtst-1.2.3 | ||
</ | </syntaxhighlight> | ||
==== Get store path for a package from the Flake input ==== | |||
When packages are managed using [[Flakes]], store paths to them can be retrieved using <code>nix eval --inputs-from</code>, like this:<syntaxhighlight lang="console"> | |||
$ nix eval --inputs-from "$flake_path" --raw "$input#$package" | |||
</syntaxhighlight>For instance, when packages are managed using [[Home Manager]] using standard configuration, store path to the [[Git]] package can be retrieved using this command:<syntaxhighlight lang="console"> | |||
$ nix eval --inputs-from ~/.config/home-manager --raw nixpkgs#git | |||
</syntaxhighlight> | |||
=== Add files to the store === | === Add files to the store === | ||
{{Warning|Editing the Nix Store manually may cause undefined behaviour to occur in Nix's database. Avoid making manual changes to the store unless you are absolutely sure you know what you're doing.}} | |||
It is sometimes necessary to add files to the store manually. | It is sometimes necessary to add files to the store manually. | ||
This is particularly the case with packages that cannot be downloaded automatically, | This is particularly the case with packages that cannot be downloaded automatically, | ||
| Line 33: | Line 42: | ||
</syntaxHighlight> | </syntaxHighlight> | ||
Unfortunately, | Unfortunately, <code>nix-store</code> will try to load the entire file into memory, | ||
which will fail if the file size exceeds available memory. | which will fail if the file size exceeds available memory. | ||
If we have root access, we can copy the file to the store ourselves: | If we have root access, we can copy the file to the store ourselves: | ||
| Line 54: | Line 63: | ||
</syntaxHighlight> | </syntaxHighlight> | ||
=== Build | === Build NixOS from nixpkgs repo === | ||
The following snippet will build the system from a git checkout: | The following snippet will build the system from a git checkout: | ||
| Line 62: | Line 71: | ||
</syntaxHighlight> | </syntaxHighlight> | ||
This method can be used when testing | This method can be used when testing NixOS services for a pull request to nixpkgs. | ||
Building | Building NixOS from a git is an alternative to using nix channels and set up permanent following this [https://web.archive.org/web/20160327190212/http://anderspapitto.com/posts/2015-11-01-nixos-with-local-nixpkgs-checkout.html blog article]. | ||
It has a couple of advantages over nixpkgs as it allows back-porting of packages/changes to stable versions | It has a couple of advantages over nixpkgs as it allows back-porting of packages/changes to stable versions | ||
as well as applying customization. | as well as applying customization. | ||
| Line 76: | Line 85: | ||
=== Evaluate a NixOS configuration without building === | === Evaluate a NixOS configuration without building === | ||
If you only want to evaluate | If you only want to evaluate <code>configuration.nix</code> without building (e.g. to syntax-check or see if you are using module options correctly), you can use: | ||
<syntaxHighlight lang="console"> | <syntaxHighlight lang="console"> | ||
| Line 82: | Line 91: | ||
</syntaxHighlight> | </syntaxHighlight> | ||
This creates the | This creates the <code>.drv</code> file that <code>nixos-rebuild build</code> would build. | ||
=== Explore a NixOS configuration in the REPL === | |||
If you want to see what ''value'' a NixOS option takes without building, as opposed to merely checking that all options work, you can run: | |||
<syntaxhighlight lang="console"> | |||
$ nix repl --file '<nixpkgs/nixos>' | |||
Welcome to Nix 2.18.2. Type :? for help. | |||
Loading installable ''... | |||
Added 6 variables. | |||
nix-repl> config.environment.shells # for example | |||
[ "/run/current-system/sw/bin/zsh" ... ] | |||
$ # Equivalently, if starting from an existing REPL: | |||
nix-repl> :l <nixpkgs/nixos> | |||
Added 6 variables. | |||
nix-repl> config.environment.shells | |||
</syntaxhighlight> | |||
This can be helpful if your configuration is spread across multiple modules, or if you import modules from external sources, or if NixOS has defaults and you want to know whether a default is being used or extended in your configuration, or a variety of other cases in which you might want the computer to tell you what the end result of all your Nixing is going to be before you switch to it. | |||
You can do this with configuration files other than the one installed in <code>/etc/nixos</code>, too: | |||
<pre> | |||
nix-repl> :a import <nixpkgs/nixos> { configuration = /path/to/config.nix; } | |||
</pre> | |||
=== Manually switching a NixOS system to a certain version of system closure === | === Manually switching a NixOS system to a certain version of system closure === | ||
| Line 135: | Line 170: | ||
where <code>-I nixpkgs=/path/to/nixpkgs</code> is optionally depending whether the vm should be build from git checkout or a channel. | where <code>-I nixpkgs=/path/to/nixpkgs</code> is optionally depending whether the vm should be build from git checkout or a channel. | ||
On non- | On non-NixOS (linux) systems the following command can be used instead: | ||
<syntaxHighlight lang="console"> | <syntaxHighlight lang="console"> | ||
| Line 155: | Line 190: | ||
</syntaxHighlight> | </syntaxHighlight> | ||
Don't forget that by default | Don't forget that by default NixOS comes with a firewall enabled: | ||
<syntaxHighlight lang="nix"> | <syntaxHighlight lang="nix"> | ||
| Line 183: | Line 218: | ||
</syntaxHighlight> | </syntaxHighlight> | ||
To initiate the build environment run | To initiate the build environment run <code>nix-shell</code> in the project root directory | ||
< | <syntaxhighlight lang="console"> | ||
# this will download add development dependencies and set up the environment so build tools will find them. | $ # this will download add development dependencies and set up the environment so build tools will find them. | ||
$ nix-shell | $ nix-shell | ||
</ | </syntaxhighlight> | ||
The following is specific to bcc or cmake in general: | The following is specific to bcc or cmake in general: | ||
(so you need to adapt the workflow depending on the project, you hack on) | (so you need to adapt the workflow depending on the project, you hack on) | ||
< | <syntaxhighlight lang="console">$ mkdir build | ||
$ mkdir build | |||
$ cd build | $ cd build | ||
# cmakeFlags is also defined in the bcc package. autotools based projects might defined $configureFlags | $ # cmakeFlags is also defined in the bcc package. autotools based projects might defined $configureFlags | ||
$ eval cmake $cmakeFlags .. | $ eval cmake $cmakeFlags .. | ||
$ make | $ make</syntaxhighlight> | ||
</ | |||
=== Evaluate packages for a different platform === | === Evaluate packages for a different platform === | ||
Sometimes you want to check whether a change to a package (such as adding a new dependency) would evaluate even on a different type of system. For example, you may want to check on | Sometimes you want to check whether a change to a package (such as adding a new dependency) would evaluate even on a different type of system. For example, you may want to check on <code>x86_64-linux</code> whether a package evaluates for <code>x86_64-darwin</code> or <code>aarch64-linux</code>. | ||
Use the | Use the <code>system</code> argument: | ||
<syntaxHighlight lang="console"> | <syntaxHighlight lang="console"> | ||
| Line 213: | Line 246: | ||
=== Cross-compile packages === | === Cross-compile packages === | ||
The following command will cross compile the tinc package for the aarch64 CPU architecture from | The following command will cross compile the <code>tinc</code> package for the aarch64 CPU architecture from the current system architecture (e.g. x86_64 if you are running a x86_64 system). | ||
< | <syntaxhighlight lang="console"> | ||
$ nix-build '<nixpkgs>' - | $ nix-build '<nixpkgs>' -A pkgsCross.aarch64-multiplatform.tinc | ||
</ | </syntaxhighlight> | ||
There are alternative ways to specify and build cross compiled packages, see [[Cross Compiling]]. | |||
=== Customizing Packages === | === Customizing Packages === | ||
| Line 235: | Line 268: | ||
</syntaxHighlight> | </syntaxHighlight> | ||
and the following in | and the following in <code>configuration.nix</code>: | ||
<syntaxHighlight lang="nix"> | <syntaxHighlight lang="nix"> | ||
| Line 319: | Line 352: | ||
using channels | using channels | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="console"> | ||
nix-build -E 'with (import <nixpkgs>{}); polybar.override { i3Support = true; }' | $ nix-build -E 'with (import <nixpkgs>{}); polybar.override { i3Support = true; }' | ||
</syntaxhighlight> | </syntaxhighlight> | ||
using a local repo | using a local repo | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="console"> | ||
nix-build -E 'with (import ./default.nix{}); polybar.override { i3Support = true; }' | $ nix-build -E 'with (import ./default.nix{}); polybar.override { i3Support = true; }' | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== See also == | == See also == | ||
* [[Garbage Collection]] | |||
* [[NFS#Nix_store_on_NFS|Nix store on NFS]] | |||
[[Category:Cookbook]] | [[Category:Cookbook]] | ||
[[Category:Software]] | [[Category:Software]] | ||