tuto:nix
Ceci est une ancienne révision du document !
Table des matières
NIX
Documentation
- Nix:
Installation
$ sh <(curl https://nixos.org/nix/install)
Pour 1 utilisateur:
$ sh <(curl https://nixos.org/nix/install) --no-daemon
Multi-utilisateurs:
$ sh <(curl https://nixos.org/nix/install) --daemon
Variables
NIX_PATH
NIX_PATH=nixpkgs=/home/gigix/.nix-defexpr/channels/nixpkgs
export NIX_PAGER=
Fichiers
config.nix
Fichier utilisateur: ~/.nixpkgs/config.nix
Commandes
nox
$ nix-env -i nox $ nox emacs
nix-env
$ nix-env -qasP hello --description $ nix-env -iA nixpkgs.hello $ nix-env -q $ nix-env -qc $ nix-env -u $ nix-env -u --dry-run $ nix-env -e hello
$ nix-env --list-generations $ nix-env --rollback switching from generation 3 to 2 $ nix-env -G 3 switching from generation 2 to 3
$ nix-env --switch-profile /nix/var/nix/profiles/gigix $ nix-env --switch-profile /nix/var/nix/profiles/default
- ~/.nixpkgs/config.nix
{ packageOverrides = pkgs: rec { homeEnv = pkgs.buildEnv { name = "homeEnv"; paths = [ pkgs.emacs24 pkgs.bsdgames ]; }; workEnv = pkgs.buildEnv { name = "workEnv"; paths = [ pkgs.emacs23 pkgs.perl ]; }; }; }
$ nix-env -p /nix/var/nix/profiles/per-user/gigix/workEnv -i workEnv $ nix-env -p /nix/var/nix/profiles/per-user/gigix/homeEnv -i homeEnv
$ nix-env -p /nix/var/nix/profiles/other-profile -i subversion
$ nix-env --delete-generations old ... removing generation 73 removing generation 74 removing generation 75 removing generation 76
nix-build
Builder psmisc en 32 bits:
$ nix-build -A psmisc --argstr system i686-linux
$ nix-build '<nixpkgs>' -A stdenv
nix-shell
$ nix-shell --pure -E 'import <nixpkgs> {}' -A hello $ nix-shell --pure -p hello --run hello
$ nix-shell '<nixpkgs>' -A pan [nix-shell]$ tar xf $src [nix-shell]$ cd pan-* [nix-shell]$ ./configure [nix-shell]$ make [nix-shell]$ ./pan/gui/pan
$ nix-shell '<nixpkgs>' -A emacs --command 'unpackPhase; patchPhase'
Custom interpreter:
#! /usr/bin/env nix-shell #! nix-shell -i real-interpreter -p packages
#! /usr/bin/env nix-shell #! nix-shell -i python -p python pythonPackages.prettytable import prettytable # Print a simple table. t = prettytable.PrettyTable(["N", "N^2"]) for n in range(1, 10): t.add_row([n, n * n]) print t
#! /usr/bin/env nix-shell #! nix-shell -i bash -p "terraform.withPlugins (plugins: [ plugins.openstack ])" terraform apply
#! /usr/bin/env nix-shell #! nix-shell -i runghc -p "haskellPackages.ghcWithPackages (ps: [ps.HTTP ps.tagsoup])" #! nix-shell -I nixpkgs=https://github.com/NixOS/nixpkgs-channels/archive/nixos-18.03.tar.gz import Network.HTTP import Text.HTML.TagSoup -- Fetch nixos.org and print all hrefs. main = do resp <- Network.HTTP.simpleHTTP (getRequest "http://nixos.org/") body <- getResponseBody resp let tags = filter (isTagOpenName "a") $ parseTags body let tags' = map (fromAttrib "href") tags mapM_ putStrLn $ filter (/= "") tags'
nix-channel
$ nix-channel --list nixpkgs http://nixos.org/channels/nixpkgs-unstable
$ nix-channel --add https://nixos.org/channels/nixos-19.09 nixpkgs_19_09 $ nix-channel --remove nixpkgs_19_09
$ nix-channel --update
nix-store
$ nix-store -q --references `which hello` /nix/store/1ncwrl8bplq3xhmj8pxfkx4y0i90vmnx-glibc-2.30 /nix/store/77gczr7dc287sqdc13gi2zcpwl7960yz-hello-2.10
$ nix-store -qR `which hello` /nix/store/mxaxvp33wg9sim8qh2kkw041v492bvxj-libunistring-0.9.10 /nix/store/hjng28vbd73qq9iz9j8r397x19aa8fp9-libidn2-2.3.0 /nix/store/1ncwrl8bplq3xhmj8pxfkx4y0i90vmnx-glibc-2.30 /nix/store/77gczr7dc287sqdc13gi2zcpwl7960yz-hello-2.10
$ nix-store -q --tree `which hello` /nix/store/77gczr7dc287sqdc13gi2zcpwl7960yz-hello-2.10 +---/nix/store/1ncwrl8bplq3xhmj8pxfkx4y0i90vmnx-glibc-2.30 | +---/nix/store/hjng28vbd73qq9iz9j8r397x19aa8fp9-libidn2-2.3.0 | | +---/nix/store/mxaxvp33wg9sim8qh2kkw041v492bvxj-libunistring-0.9.10 | | | +---/nix/store/mxaxvp33wg9sim8qh2kkw041v492bvxj-libunistring-0.9.10 [...] | | +---/nix/store/hjng28vbd73qq9iz9j8r397x19aa8fp9-libidn2-2.3.0 [...] | +---/nix/store/1ncwrl8bplq3xhmj8pxfkx4y0i90vmnx-glibc-2.30 [...] +---/nix/store/77gczr7dc287sqdc13gi2zcpwl7960yz-hello-2.10 [...]
$ nix-store -q --referrers `which hello` /nix/store/77gczr7dc287sqdc13gi2zcpwl7960yz-hello-2.10 /nix/store/mbd3a74yrg7qvlv5jqz9fss6lkqd314c-env-manifest.nix /nix/store/x3asfl3l7zn7lnwkbz5iahs789zk9lx1-user-environment
Show all paths that depend on the same OpenSSL library as svn:
Afficher tous les chemins qui dépendent de la même bibliothèque OpenSSL que svn:
$ nix-store -q --referrers $(nix-store -q --binding openssl $(nix-store -qd $(which svn))) /nix/store/23ny9l9wixx21632y2wi4p585qhva1q8-sylpheed-1.0.0 /nix/store/5mbglq5ldqld8sj57273aljwkfvj22mc-subversion-1.1.4 /nix/store/dpmvp969yhdqs7lm2r1a3gng7pyq6vy4-subversion-1.1.3 /nix/store/l51240xqsgg8a7yrbqdx1rfzyv6l26fx-lynx-2.8.5
Pour un paquet non installé:
$ nix-store --query --references $(nix-instantiate '<nixpkgs>' -A emacs)
Vérifier le store:
$ nix-store --verify --check-contents reading the Nix store... checking path existence... checking hashes...
Réparer le store:
$ nix-store --verify --check-contents --repair
Il est parfois nécessaire de modifier le store manuellement:
$ nix-store --add-fixed sha256 /path/to/file
Optimiser le store pour gagner en place:
$ nix-store --optimise
Suppression d'une dérivation non désirée:
$ nix-store --delete /nix/store/31icg8jxiyxkcmx8gwak3fvq9shi6fd8-hello-2.8 finding garbage collector roots... removing stale link from '/nix/var/nix/gcroots/auto/8m1kkl216mxi8yn9pgn009838qil7mlv' to '/etc/nixos/result' deleting '/nix/store/31icg8jxiyxkcmx8gwak3fvq9shi6fd8-hello-2.8' deleting '/nix/store/trash' deleting unused links... note: currently hard linking saves -0.00 MiB 1 store paths deleted, 0.18 MiB freed
nix-collect-garbage
C'est un alias de nix-store –gc
.
$ nix-collect-garbage ... deleting '/nix/store/gazpv90zhld15svbkbfi17nd182f63nm-webencodings-0.5.1.tar.gz.drv' deleting '/nix/store/la6n976p7nmk9ijkjff2i040nc24knql-setenv-0.1.1.3-r1.cabal.drv' deleting '/nix/store/g85ssgjv1i5djy8589444fhlyxnhnqcp-splitmix-0.0.3-r1.cabal.drv' deleting '/nix/store/trash' deleting unused links... note: currently hard linking saves -0.00 MiB 2419 store paths deleted, 2308.54 MiB freed
$ nix-collect-garbage--delete-older-than 30d
nix-prefetch-url
$ nix-prefetch-url --unpack https://github.com/gigi206/salt/archive/742781c358d65c8849a6a6f5b8f4bb3dcefc203a.tar.gz unpacking... [14.5 MiB DL] path is '/nix/store/2l5az5rvkkavrr7zgghgqpmsqj3vn1yp-742781c358d65c8849a6a6f5b8f4bb3dcefc203a.tar.gz' 02cgs6b1rmh8hmcwm5wpxxg32p3km9c05606pykv49qq1spd6p38
nix
nix show-derivation
$ nix show-derivation '((import <nixpkgs> {}).hello)' { "/nix/store/17abmzvc6qq1yhi01aac11jpznrnndqv-hello-2.10.drv": { "outputs": { "out": { "path": "/nix/store/77gczr7dc287sqdc13gi2zcpwl7960yz-hello-2.10" } }, "inputSrcs": [ "/nix/store/9krlzvny65gdc8s7kpb6lkx8cd02c25b-default-builder.sh" ], "inputDrvs": { "/nix/store/7cipdvw2s5sz1px8x3hv41j40753969s-bash-4.4-p23.drv": [ "out" ], "/nix/store/hzd53xaaqkh0jib90hv31ixjgxrlppi9-stdenv-linux.drv": [ "out" ], "/nix/store/j5zjn5ymk44wyf27gfgfwgs4yj2pylnz-hello-2.10.tar.gz.drv": [ "out" ] }, "platform": "x86_64-linux", "builder": "/nix/store/1iaxkm0941nj1m4m5g4fxgg4cq5jckf0-bash-4.4-p23/bin/bash", "args": [ "-e", "/nix/store/9krlzvny65gdc8s7kpb6lkx8cd02c25b-default-builder.sh" ], "env": { "buildInputs": "", "builder": "/nix/store/1iaxkm0941nj1m4m5g4fxgg4cq5jckf0-bash-4.4-p23/bin/bash", "configureFlags": "", "depsBuildBuild": "", "depsBuildBuildPropagated": "", "depsBuildTarget": "", "depsBuildTargetPropagated": "", "depsHostHost": "", "depsHostHostPropagated": "", "depsTargetTarget": "", "depsTargetTargetPropagated": "", "doCheck": "1", "doInstallCheck": "", "name": "hello-2.10", "nativeBuildInputs": "", "out": "/nix/store/77gczr7dc287sqdc13gi2zcpwl7960yz-hello-2.10", "outputs": "out", "patches": "", "pname": "hello", "propagatedBuildInputs": "", "propagatedNativeBuildInputs": "", "src": "/nix/store/3x7dwzq014bblazs7kq20p9hyzz0qh8g-hello-2.10.tar.gz", "stdenv": "/nix/store/0w5454az7vwcq60yqvcsv8fs9q7r4zrx-stdenv-linux", "strictDeps": "", "system": "x86_64-linux", "version": "2.10" } } }
nix run
$ nix run nixpkgs.hello --command hello
Equivalent à:
$ nix-shell --pure -p hello --run hello
nix search
$ nix search hello
nix path-info
$ nix path-info --recursive --closure-size nixpkgs.hello | sort --numeric-sort --key 2 /nix/store/mxaxvp33wg9sim8qh2kkw041v492bvxj-libunistring-0.9.10 1634416 /nix/store/hjng28vbd73qq9iz9j8r397x19aa8fp9-libidn2-2.3.0 1857088 /nix/store/1ncwrl8bplq3xhmj8pxfkx4y0i90vmnx-glibc-2.30 32939352 /nix/store/77gczr7dc287sqdc13gi2zcpwl7960yz-hello-2.10 33145320
$ readlink -f $(which hello)
nix repl
$ nix repl '<nixpkgs>' Welcome to Nix version 2.3.3. Type :? for help. $ nix-repl> builtins.currentSystem "x86_64-linux"
$ nix-repl> :l <nixpkgs> nix-repl> simple = derivation { name = "simple"; builder = "${bash}/bin/bash"; args = [ ./simple_builder.sh ]; gcc = gcc; coreutils = coreutils; src = ./simple.c; system = builtins.currentSystem; } nix-repl> :b simple this derivation produced the following outputs: out -> /nix/store/ni66p4jfqksbmsl616llx3fbs1d232d4-simple
nix language
Nombres (int / float)
$ nix eval '(1 + 1)'
$ nix eval '(builtins.typeOf 1)' "int" $ nix eval '(builtins.typeOf 1.0)' "float"
Chaines (string)
$ nix eval "('' line 1 line 2 line 3 '')"
$ nix eval '("a" + "b")' "ab"
variables (let)
$ nix eval '(let a = 1; b = 2; in a + b)' 3 $ nix eval '( let foo = "Foo"; bar = "Bar"; in "${foo + bar} is a terrible name")' "FooBar is a terrible name"
Conditions (if)
nix-repl> a = 3 nix-repl> b = 4 nix-repl> if a > b then "yes" else "no" "no"
Fonctions (lambda)
$ nix eval '((a: a + 1) 2)' 3
Listes (list)
$ nix eval '([1 2] ++ [3 4])' [ 1 2 3 4 ]
Set
$ nix eval '({ foo = 1; bar = 2; }.bar)' 2 $ nix eval '({ foo = 1; bar = 2; } // { bar = 3; baz = 4; })' { bar = 3; baz = 4; foo = 1; } $ nix eval '(rec { foo = bar; bar = 2; }.foo)' 2 $ nix eval '(({ foo, bar }: foo + bar ) { foo = 1; bar = 2; })' 3 $ nix eval '(({ foo, ...}: foo + 2 ) { foo = 3; bar = 4; })' 5 $ nix eval '((s@{ foo, bar }: foo + s.bar ) { foo = 2; bar = 3; }) 5 $ nix eval '(let a = 3; in { a = a; })' { a = 3; } $ let a = 3; in { b = a; inherit a; })' { a = 3; b = 3; }
Chemains (path)
$ nix eval '(some/path)' /home/gigix/some/path $ nix eval '(./some/path)' /home/gigix/some/path $ nix eval '(/some/path)' /some/path $ nix eval '(<nixpkgs>)' /home/gigix/.nix-defexpr/channels/nixpkgs
Imports
$ nix eval '(import <nixpkgs>)' <LAMBDA>
Restaurer le binaire nix (recovery)
$ nix-env -e '*' uninstalling 'hello-2.10' uninstalling 'nix-2.1.3' [...] $ ls -l /nix/store/*-nix-*/bin/nix-env lrwxrwxrwx. 1 gigix gigix 3 1 janv. 1970 /nix/store/ddmmzn4ggz1f66lwxjy64n89864yj9w9-nix-2.3.3/bin/nix-env -> nix $ /nix/store/ddmmzn4ggz1f66lwxjy64n89864yj9w9-nix-2.3.3/bin/nix-env -i /nix/store/ddmmzn4ggz1f66lwxjy64n89864yj9w9-nix-2.3.3/bin/nix-env
Dérivations
Simple dérivation
- simple1.nix
with (import <nixpkgs> {}); derivation { name = "simple"; builder = "${bash}/bin/bash"; args = [ ./simple_builder.sh ]; inherit gcc coreutils; src = ./simple.c; system = builtins.currentSystem; }
- simple2.nix
with (import <nixpkgs> {}); derivation { name = "hello"; builder = "${bash}/bin/bash"; args = [ ./hello_builder.sh ]; inherit gnutar gzip gnumake gcc coreutils gawk gnused gnugrep; binutils = binutils-unwrapped; src = ./hello-2.10.tar.gz; system = builtins.currentSystem; }
Créer un fichier de fonction autotools.nix
:
- autotools.nix
pkgs: attrs: with pkgs; let defaultAttrs = { builder = "${bash}/bin/bash"; args = [ ./builder.sh ]; baseInputs = [ gnutar gzip gnumake gcc binutils-unwrapped coreutils gawk gnused gnugrep ]; buildInputs = []; system = builtins.currentSystem; }; in derivation (defaultAttrs // attrs)
- autotools.nix
pkgs: attrs: with pkgs; let defaultAttrs = { builder = "${bash}/bin/bash"; args = [ ./builder.sh ]; baseInputs = [ gnutar gzip gnumake gcc binutils-unwrapped coreutils gawk gnused gnugrep ]; buildInputs = []; system = builtins.currentSystem; }; in derivation (defaultAttrs // attrs)
nix-repl> { a = "b"; } // { c = "d"; } { a = "b"; c = "d"; } nix-repl> { a = "b"; } // { a = "c"; } { a = "c"; }
- hello.nix
let pkgs = import <nixpkgs> {}; mkDerivation = import ./autotools.nix pkgs; in mkDerivation { name = "hello"; src = ./hello-2.10.tar.gz; }
Override
$ nix-repl nix-repl> :l <nixpkgs> Added 4360 variables. nix-repl> :b graphviz.override { xlibs = null; }
{ packageOverrides = pkgs: { graphviz = pkgs.graphviz.override { xlibs = null; }; }; }
buildInputs
$ nix-build exemple1.nix
- exemple1.nix
let nixpkgs = import <nixpkgs> {}; inherit (nixpkgs) stdenv fetchurl which; actualHello = stdenv.mkDerivation { name = "hello-2.3"; src = fetchurl { url = mirror://gnu/hello/hello-2.3.tar.bz2; sha256 = "0c7vijq8y68bpr7g6dh1gny0bff8qq81vnp4ch8pjzvg56wb3js1"; }; }; wrappedHello = stdenv.mkDerivation { name = "hello-wrapper"; buildInputs = [ actualHello which ]; unpackPhase = "true"; installPhase = '' mkdir -p "$out/bin" echo "#! ${stdenv.shell}" >> "$out/bin/hello" echo "exec $(which hello)" >> "$out/bin/hello" ''; }; in wrappedHell
propagatedBuildInputs
$ nix-build exemple2.nix
- exemple2.nix
let nixpkgs = import <nixpkgs> {}; inherit (nixpkgs) stdenv fetchurl which; actualHello = stdenv.mkDerivation { name = "hello-2.3"; src = fetchurl { url = mirror://gnu/hello/hello-2.3.tar.bz2; sha256 = "0c7vijq8y68bpr7g6dh1gny0bff8qq81vnp4ch8pjzvg56wb3js1"; }; }; intermediary = stdenv.mkDerivation { name = "middle-man"; propagatedBuildInputs = [ actualHello ]; unpackPhase = "true"; installPhase = '' mkdir -p "$out" ''; }; wrappedHello = stdenv.mkDerivation { name = "hello-wrapper"; buildInputs = [ intermediary which ]; unpackPhase = "true"; installPhase = '' mkdir -p "$out/bin" echo "#! ${stdenv.shell}" >> "$out/bin/hello" echo "exec $(which hello)" >> "$out/bin/hello" ''; }; in wrappedHello
shellHook
- test.nix
with import <nixpkgs> {}; stdenv.mkDerivation { name = "my-environment"; buildInputs = [ pkgs.figlet pkgs.lolcat ]; # The '' quotes are 2 single quote characters # They are used for multi-line strings shellHook = '' figlet "Welcome!" | lolcat --freq 0.5 ''; }
$ nix-shell test.nix __ __ _ _ \ \ / /__| | ___ ___ _ __ ___ ___| | \ \ /\ / / _ \ |/ __/ _ \| '_ ` _ \ / _ \ | \ V V / __/ | (_| (_) | | | | | | __/_| \_/\_/ \___|_|\___\___/|_| |_| |_|\___(_)
tuto/nix.1583519214.txt.gz · Dernière modification : 2020/03/06 18:26 de root