https://github.com/nix-community/nix-user-chroot
Outil qui permet de donner un répertoire qui sera présenter comme point de montage /nix
grâce au namespace en espace utilisateur:
$ mkdir -m 0755 ~/.nix $ nix-user-chroot ~/.nix ${SHELL=bash} -l
$ 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
NIX_PATH=nixpkgs=/home/gigix/.nix-defexpr/channels/nixpkgs
export NIX_PAGER=
nixpkgs=https://github.com/NixOS/nixpkgs-channels/archive/nixos-15.09.tar.gz nixpkgs=channel:nixos-15.09
Fichier utilisateur: ~/.nixpkgs/config.nix
$ nix-env -i nox $ nox emacs
$ 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
Force a specific channel (master in this example):
$ nix-env -f https://github.com/NixOS/nixpkgs/archive/master.tar.gz -iA 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
{ 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
Builder psmisc en 32 bits:
$ nix-build -A psmisc --argstr system i686-linux
$ nix-build '<nixpkgs>' -A stdenv
{ pkgs ? import <nixpkgs> {} }: with pkgs; mkShell { buildInputs = [ elixir_1_10 nodejs-12_x ]; }
$ nix-shell $ nix-shell shell.nix
$ 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'
Force a specific channel:
$ nix-shell -p hello -I nixpkgs=https://github.com/NixOS/nixpkgs/archive/master.tar.gz
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 --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
Master channel:
$ nix-channel --add https://github.com/NixOS/nixpkgs/archive/master.tar.gz master
$ nix-channel --update
$ 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
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 --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-instantiate --eval -E '(import <nixpkgs> {}).vscode.version' "1.43.0"
$ nix-instantiate --eval -E '(import <nixpkgs> {}).lib.version' "20.09pre216928.991bbef6835"
$ 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 nixpkgs.hello --command hello
Equivalent à:
$ nix-shell --pure -p hello --run hello
$ nix search hello
$ 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 '<nixpkgs>' Welcome to Nix version 2.3.3. Type :? for help. nix-repl> builtins.currentSystem "x86_64-linux"
$ nix repl 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-repl> (builtins.parseDrvName "NUnit.Console-3.0.1").name "NUnit.Console"
Permet d'éditer un build avant de l'installer ([|https://nixos.org/manual/nix/stable/command-ref/new-cli/nix3-edit.html|nix edit]]):
$ EDITOR=vim nix edit -f "<nixpkgs>" samba --extra-experimental-features nix-command
On install ensuite le paquet:
$ nix-env -i samba
$ nix eval '(1 + 1)'
$ nix eval '(builtins.typeOf 1)' "int" $ nix eval '(builtins.typeOf 1.0)' "float"
$ nix eval "('' line 1 line 2 line 3 '')"
$ nix eval '("a" + "b")' "ab"
$ 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"
nix-repl> a = 3 nix-repl> b = 4 nix-repl> if a > b then "yes" else "no" "no"
$ nix eval '((a: a + 1) 2)' 3
$ nix eval '([1 2] ++ [3 4])' [ 1 2 3 4 ]
$ 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; }
$ 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
$ nix eval '(import <nixpkgs>)' <LAMBDA>
$ 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
with (import <nixpkgs> {}); derivation { name = "simple"; builder = "${bash}/bin/bash"; args = [ ./simple_builder.sh ]; inherit gcc coreutils; src = ./simple.c; system = builtins.currentSystem; }
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
:
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)
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"; }
let pkgs = import <nixpkgs> {}; mkDerivation = import ./autotools.nix pkgs; in mkDerivation { name = "hello"; src = ./hello-2.10.tar.gz; }
$ nix repl nix-repl> :l <nixpkgs> Added 4360 variables. nix-repl> :b graphviz.override { xlibs = null; }
{ packageOverrides = pkgs: { graphviz = pkgs.graphviz.override { xlibs = null; }; }; }
$ nix-build 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
$ nix-build 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
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 / __/ | (_| (_) | | | | | | __/_| \_/\_/ \___|_|\___\___/|_| |_| |_|\___(_)
{ stdenv, fetchurl, openmpi }: stdenv.mkDerivation rec { name = "oned"; src = fetchurl { url = "https://www.pdc.kth.se/education/tutorials/mpi/hybrid-lab/oned.c"; sha256 = "<HASH_HERE>"; }; buildInputs = [ openmpi ]; builder = builtins.toFile "builder.sh" " source $stdenv/setup mpicc -w -o oned.exe $src mkdir $out mkdir $out/bin cp oned.exe $out/bin "; meta = { description = "JDEV 2017 Nix tutoriel"; license = stdenv.lib.licenses.gpl2; platforms = stdenv.lib.platforms.unix; }; }
Pour ajouter un paquet personnel ou pour modifier un paquet déjà existant, Nix permet d'ajouter des overlays.
Il s'agit de fichiers Nix que l'on place dans le dossier ~/.config/nixpkgs/overlays/
. Ces overlays sont alors appliqués automatiquement sur la logithèque.
Si on ajoute le fichier ~/.config/nixpkgs/overlays/monOverlay1.nix
suivant, on modifie le paquet boost de la logithèque et on y ajoute un paquet monAppli:
self: super: { boost = super.boost.override { python = self.python3; }; monAppli = super.callPackage ./pkgs/monAppli.nix {}; }
Dans cet overlay, self
et super
sont les paramètres de la fonction à appliquer sur la logithèque d’entrée, super
est la version initiale de la logithèque et self
la version modifiée.
Ces modifications seront alors automatiquement appliquées, par exemple si on installe un de ces paquets ou si on lance un nix-shell qui les utilisent.
self: super: { nano-no-nls = super.nano.override { enableNls = false; }; }
Nix permet de modifier les paquets encore plus profondément. Par exemple, on peut modifier le paquet existant nano de façon à utiliser la version 4.5 du code source de nano:
self: super: { nano = super.nano.overrideAttrs (oldAttrs: rec { pname = oldAttrs.pname; version = "4.5"; src = super.fetchurl { url = "mirror://gnu/nano/${pname}-${version}.tar.xz"; sha256 = "0czmz1yq8s5qcxcmfjdxzg9nkhbmlc9q1nz04jvf57fdbs7w7mfy"; }; }); }
Le but d'home-manager est de fournir l'équivalent du fichier /etc/nixos/configuration.nix
mais pour l'environnement utilisateur (au lieu de l'environnement système). Plus précisemment, l'utilisateur décrit sa configuration via un fichier ~/.config/nixpkgs/home.nix
et lance une commande home-manager pour construire et installer la configuration correspondante.
$ nix-env -iA nixos.home-manager
{ pkgs, ... }: { home.packages = with pkgs; [ geany meld vlc ]; home.keyboard = { layout = "fr"; variant = "bepo"; }; }
Prise en compte de la configuration:
$ home-manager switch
Séparer:
{ pkgs, ... }: { home.packages = with pkgs; [ geany meld vlc ]; }
{ pkgs, ... }: { imports = [ ./packages.nix ]; home.keyboard = { layout = "fr"; variant = "bepo"; }; }
Autre exemple:
{ pkgs, ... }: { programs = { firefox.enable = true; .git = { enable = true; userName = "gigix"; userEmail = "gigix@example.com"; ignores = [ "*~" "*.swp" ]; }; bash = { enable = true; shellAliases = { ll = "ls -lh"; la = "ls -a"; }; }; }; }