Nix: What is it and what should I use it with?







Hello, Habr!







At Typeable, we wanted to publish a short series of articles on how Nix helps us (and hinders us a little) in development. But after spending a little time looking for similar material here, we were surprised to find that there is no sensible introduction to Nix on Habré to which one could refer.







@snizovtsev C++, , . :)







.







?



NixOS, , Nix ( ) Linux. :







$ sh <(curl -L https://nixos.org/nix/install)
      
      





. MacOS, , . MacOS .







Nix



Nix, : Nix nixpkgs , NixOS. .







Nix — . ML (SML, OCaml, Haskell), , , .







.







$ nix repl
Welcome to Nix version 2.3.10. Type :? for help.

nix-repl> 
      
      





Nix . , .







nix-repl> "Hello " + "World!"
"Hello World!"

nix-repl> add = a: b: a + b

nix-repl> add 1 2
3
      
      





, Nix, .







nix-repl> addOne = add 1

nix-repl> addOne 3
4
      
      





, , Nix (attribute sets Nix).







nix-repl> list = [ 1 2 3 ]

nix-repl> set = { a = 1; b = list; }     

nix-repl> set
{ a = 1; b = [ ... ]; }

nix-repl> set.b
[ 1 2 3 ]
      
      





let...in



. , , , .







fac.nix



:







let
  fac = n:
    if n == 0
    then 1
    else n * fac (n - 1);
in { inherit fac; }
      
      





inherit



"" . let fac = ... in { fac = fac; }



.







$ nix repl fac.nix
Welcome to Nix version 2.3.10. Type :? for help.

Loading 'fac.nix'...
Added 1 variables.

nix-repl> fac 3 
6
      
      





REPL, Nix , , .







Nix import



, .







mul.nix



:







let
  mul = a: b: a * b;
in { inherit mul; }
      
      





fac.nix



:







let
  multMod = import ./mul.nix;
  fac = n:
    if n == 0
    then 1
    else multMod.mul n (fac (n - 1));
in { inherit fac; }
      
      





— , , ? Nix with



, , .







fac.nix



with



:







with import ./mul.nix;
let
  fac = n:
    if n == 0
    then 1
    else mul n (fac (n - 1));
in { inherit fac; }
      
      







— Nix.







, , , Derivation



. Derivation



— , - . C, "Hello World!", derivation :







Derive([("out","/nix/store/1nq46fyv3629slgxnagqn2c01skp7xrq-hello-world","","")],[("/nix/store/60xqp516mkfhf31n6ycyvxppcknb2dwr-build-hello.drv",["out"])],["/nix/store/wiviq2xyz0ylhl0qcgfgl9221nkvvxfj-hello.c"],"x86_64-linux","/nix/store/r5lh8zg768swlm9hxxfrf9j8gwyadi72-build-hello",[],[("builder","/nix/store/r5lh8zg768swlm9hxxfrf9j8gwyadi72-build-hello"),("name","hello-world"),("out","/nix/store/1nq46fyv3629slgxnagqn2c01skp7xrq-hello-world"),("src","/nix/store/wiviq2xyz0ylhl0qcgfgl9221nkvvxfj-hello.c"),("system","x86_64-linux")])
      
      





, , , , , : . , /nix/store



. , Nix , (sandbox). .







, ! , Nix derivation



, .







simple-derivation/default.nix



:







{ pkgs ? import <nixpkgs> {} }:

derivation {
  name = "hello-world";
  builder = pkgs.writeShellScript "build-hello" ''
    ${pkgs.coreutils}/bin/mkdir -p $out/bin
    ${pkgs.gcc}/bin/gcc $src -o $out/bin/hello -O2
  '';
  src = ./hello.c;
  system = builtins.currentSystem;
}
      
      





. , — , pkgs



. , : import <nixpkgs> {}



.







derivation



— , : name



— , builder



— , src



— , system



— , .







writeShellScript



nixpkgs



, . Nix .







nix build



, .







$ nix build -f ./simple-derivation/default.nix
[1 built]

$ ./result/bin/hello 
Hello World!
      
      





nix build



, result



, /nix/store



.







$ ls -l result 
lrwxrwxrwx 1 user users 50 Mar 29 17:53 result -> /nix/store/vpcddray35g2jrv40dg1809xrmz73awi-simple

$ find /nix/store/vpcddray35g2jrv40dg1809xrmz73awi-simple
/nix/store/vpcddray35g2jrv40dg1809xrmz73awi-simple
/nix/store/vpcddray35g2jrv40dg1809xrmz73awi-simple/bin
/nix/store/vpcddray35g2jrv40dg1809xrmz73awi-simple/bin/hello
      
      





,



derivation



— , Nix . , cowsay



.







{ lib, stdenv, fetchurl, perl }:

stdenv.mkDerivation rec {
  version = "3.03+dfsg2";
  pname = "cowsay";

  src = fetchurl {
    url = "http://http.debian.net/debian/pool/main/c/cowsay/cowsay_${version}.orig.tar.gz";
    sha256 = "0ghqnkp8njc3wyqx4mlg0qv0v0pc996x2nbyhqhz66bbgmf9d29v";
  };

  buildInputs = [ perl ];

  postBuild = ''
    substituteInPlace cowsay --replace "%BANGPERL%" "!${perl}/bin/perl" \
      --replace "%PREFIX%" "$out"
  '';

  installPhase = ''
    mkdir -p $out/{bin,man/man1,share/cows}
    install -m755 cowsay $out/bin/cowsay
    ln -s cowsay $out/bin/cowthink
    install -m644 cowsay.1 $out/man/man1/cowsay.1
    ln -s cowsay.1 $out/man/man1/cowthink.1
    install -m644 cows/* -t $out/share/cows/
  '';

  meta = with lib; {
    description = "A program which generates ASCII pictures of a cow with a message";
    homepage = "https://en.wikipedia.org/wiki/Cowsay";
    license = licenses.gpl1;
    platforms = platforms.all;
    maintainers = [ maintainers.rob ];
  };
}
      
      





.







stdenv



derivation



, : , . — setup



, builder



.







 $ nix build nixpkgs.stdenv

 $ find result/
result/
result/setup
result/nix-support

$ wc -l result/setup 
1330 result/setup
      
      





mkDerivation



— , derivation



.







, Arch Linux Gentoo, . , , (buildInputs



) .









Nix . , Nix Typeable, . Stay tuned!







, Nix Nix pills.








All Articles