Outils pour utilisateurs

Outils du site


tuto:nix

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentesRévision précédente
Prochaine révision
Révision précédente
tuto:nix [2020/03/06 18:26] – [nix-store] roottuto:nix [2022/06/21 20:15] (Version actuelle) – [nix-shell] root
Ligne 15: Ligne 15:
       * [[https://nixos.wiki/index.php?title=Cheatsheet&useskin=vector|Cheatsheet]]       * [[https://nixos.wiki/index.php?title=Cheatsheet&useskin=vector|Cheatsheet]]
  
 +===== nix-user-chroot =====
 +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:
 +<xtermrtf>
 +$ mkdir -m 0755 ~/.nix
 +$ nix-user-chroot ~/.nix ${SHELL=bash} -l
 +</xtermrtf>
 ===== Installation ===== ===== Installation =====
 <xtermrtf> <xtermrtf>
Ligne 36: Ligne 44:
 </xtermrtf> </xtermrtf>
  
 +==== NIX_PAGER ====
 <xtermrtf> <xtermrtf>
 export NIX_PAGER= export NIX_PAGER=
 +</xtermrtf>
 +
 +==== nixpkgs ====
 +<xtermrtf>
 +nixpkgs=https://github.com/NixOS/nixpkgs-channels/archive/nixos-15.09.tar.gz
 +nixpkgs=channel:nixos-15.09
 </xtermrtf> </xtermrtf>
  
Ligne 59: Ligne 74:
 $ nix-env -u --dry-run $ nix-env -u --dry-run
 $ nix-env -e hello $ nix-env -e hello
 +</xtermrtf>
 +
 +Force a specific channel (master in this example):
 +<xtermrtf>
 +$ nix-env -f https://github.com/NixOS/nixpkgs/archive/master.tar.gz -iA hello
 </xtermrtf> </xtermrtf>
  
Ligne 120: Ligne 140:
 ==== nix-shell ==== ==== nix-shell ====
   * https://www.sam.today/blog/environments-with-nix-shell-learning-nix-pt-1/   * https://www.sam.today/blog/environments-with-nix-shell-learning-nix-pt-1/
 +
 +<code haskell shell.nix>
 +{ pkgs ? import <nixpkgs> {} }:
 +
 +with pkgs;
 +
 +mkShell {
 +  buildInputs = [
 +    elixir_1_10
 +    nodejs-12_x
 +  ];
 +}
 +</code>
 +
 +<xtermrtf>
 +$ nix-shell
 +$ nix-shell shell.nix
 +</xtermrtf>
 +
 <xtermrtf> <xtermrtf>
 $ nix-shell --pure -E 'import <nixpkgs> {}' -A hello $ nix-shell --pure -E 'import <nixpkgs> {}' -A hello
Ligne 136: Ligne 175:
 <xtermrtf> <xtermrtf>
 $ nix-shell '<nixpkgs>' -A emacs --command 'unpackPhase; patchPhase' $ nix-shell '<nixpkgs>' -A emacs --command 'unpackPhase; patchPhase'
 +</xtermrtf>
 +
 +Force a specific channel:
 +<xtermrtf>
 +$ nix-shell -p hello -I nixpkgs=https://github.com/NixOS/nixpkgs/archive/master.tar.gz
 </xtermrtf> </xtermrtf>
  
Ligne 189: Ligne 233:
 $ nix-channel --add https://nixos.org/channels/nixos-19.09 nixpkgs_19_09 $ nix-channel --add https://nixos.org/channels/nixos-19.09 nixpkgs_19_09
 $ nix-channel --remove nixpkgs_19_09 $ nix-channel --remove nixpkgs_19_09
 +</xtermrtf>
 +
 +Master channel:
 +<xtermrtf>
 +$ nix-channel --add https://github.com/NixOS/nixpkgs/archive/master.tar.gz master
 </xtermrtf> </xtermrtf>
  
Ligne 306: Ligne 355:
 path is '/nix/store/2l5az5rvkkavrr7zgghgqpmsqj3vn1yp-742781c358d65c8849a6a6f5b8f4bb3dcefc203a.tar.gz' path is '/nix/store/2l5az5rvkkavrr7zgghgqpmsqj3vn1yp-742781c358d65c8849a6a6f5b8f4bb3dcefc203a.tar.gz'
 02cgs6b1rmh8hmcwm5wpxxg32p3km9c05606pykv49qq1spd6p38 02cgs6b1rmh8hmcwm5wpxxg32p3km9c05606pykv49qq1spd6p38
 +</xtermrtf>
 +
 +==== nix-instantiate ====
 +<xtermrtf>
 +$ nix-instantiate --eval -E '(import <nixpkgs> {}).vscode.version'
 +"1.43.0"
 +</xtermrtf>
 +
 +<xtermrtf>
 +$ nix-instantiate --eval -E '(import <nixpkgs> {}).lib.version'
 +"20.09pre216928.991bbef6835"
 </xtermrtf> </xtermrtf>
  
Ligne 408: Ligne 468:
 Welcome to Nix version 2.3.3. Type :? for help. Welcome to Nix version 2.3.3. Type :? for help.
  
-nix-repl> builtins.currentSystem+nix-repl> builtins.currentSystem
 "x86_64-linux" "x86_64-linux"
 </xtermrtf> </xtermrtf>
  
 <xtermrtf> <xtermrtf>
-$ nix-repl> :l <nixpkgs>+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> 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 nix-repl> :b simple
 this derivation produced the following outputs: out -> /nix/store/ni66p4jfqksbmsl616llx3fbs1d232d4-simple this derivation produced the following outputs: out -> /nix/store/ni66p4jfqksbmsl616llx3fbs1d232d4-simple
 +</xtermrtf>
 +
 +<xtermrtf>
 +nix-repl> (builtins.parseDrvName "NUnit.Console-3.0.1").name
 +"NUnit.Console"
 +</xtermrtf>
 +
 +=== nix edit ===
 +Permet d'éditer un build avant de l'installer ([|https://nixos.org/manual/nix/stable/command-ref/new-cli/nix3-edit.html|nix edit]]):
 +<xtermrtf>
 +$ EDITOR=vim nix edit -f "<nixpkgs>" samba --extra-experimental-features nix-command
 +</xtermrtf>
 +
 +On install ensuite le paquet:
 +<xtermrtf>
 +$ nix-env -i samba
 </xtermrtf> </xtermrtf>
  
Ligne 497: Ligne 574:
 </xtermrtf> </xtermrtf>
  
-==== Chemains (path) ====+==== Chemins (path) ====
 <xtermrtf> <xtermrtf>
 $ nix eval '(some/path)'  $ nix eval '(some/path)' 
Ligne 601: Ligne 678:
 ==== Override ==== ==== Override ====
 <xtermrtf> <xtermrtf>
-$ nix-repl+$ nix repl
 nix-repl> :l <nixpkgs> nix-repl> :l <nixpkgs>
 Added 4360 variables. Added 4360 variables.
Ligne 733: Ligne 810:
 </xtermrtf> </xtermrtf>
  
 +==== builder ====
 +
 +<xtermrtf>
 +{ 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;
 +  };
 +}
 +</xtermrtf>
 +
 +===== Overlays =====
 +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**:
 +<code nix ~/.config/nixpkgs/overlays/monOverlay1.nix>
 +self: super: {
 +
 +  boost = super.boost.override {
 +    python = self.python3;
 +  };
 +
 +  monAppli = super.callPackage ./pkgs/monAppli.nix {};
 +
 +}
 +</code>
 +
 +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.
 +
 +<code>
 +self: super: {
 +
 +  nano-no-nls = super.nano.override {
 +    enableNls = false;
 +  };
 +}
 +</code>
 +
 +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:
 +
 +<code>
 +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";
 +    };
 +  });
 +
 +}
 +</code>
 +
 +===== home-manager =====
 +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.
 +
 +<xtermrtf>
 +$ nix-env -iA nixos.home-manager
 +</xtermrtf>
 +
 +<code nix ~/.config/nixpkgs/home.nix>
 +{ pkgs, ... }: {
 +
 +  home.packages = with pkgs; [
 +    geany
 +    meld
 +    vlc
 +  ];
 +
 +  home.keyboard = {
 +    layout = "fr";
 +    variant = "bepo";
 +  };
 +
 +}
 +</code>
 +
 +Prise en compte de la configuration:
 +<xtermrtf>
 +$ home-manager switch
 +</xtermrtf>
 +
 +Séparer:
 +<code nix ~/.config/nixpkgs/packages.nix>
 +{ pkgs, ... }: {
 +
 +  home.packages = with pkgs; [
 +    geany
 +    meld
 +    vlc
 +  ];
 +
 +}
 +</code>
 +
 +<code nix ~/.config/nixpkgs/home.nix>
 +{ pkgs, ... }: {
 +
 +  imports = [
 +    ./packages.nix
 +  ];
 +
 +  home.keyboard = {
 +    layout = "fr";
 +    variant = "bepo";
 +  };
 +}
 +</code>
 +
 +Autre exemple:
 +<code nix>
 +{ 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";
 +      };
 +    };
 +  };
 +}
 +</code>
tuto/nix.1583519214.txt.gz · Dernière modification : 2020/03/06 18:26 de root