Cheatsheet: Difference between revisions
No edit summary |
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 |
||
| (8 intermediate revisions by 5 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 ==== | ==== 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=" | 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" | $ 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=" | </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 | $ nix eval --inputs-from ~/.config/home-manager --raw nixpkgs#git | ||
</syntaxhighlight> | </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 93: | Line 95: | ||
=== Explore a NixOS configuration in the REPL === | === 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: | 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 | $ nix repl --file '<nixpkgs/nixos>' | ||
nix-repl> : | 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. | Added 6 variables. | ||
nix-repl> config.environment.shells | 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. | 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. | ||
| Line 210: | Line 220: | ||
To initiate the build environment run <code>nix-shell</code> in the project root directory | 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 === | ||
| Line 238: | 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 344: | 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> | ||