Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Nixについて

mt_caret
October 08, 2017

 Nixについて

mt_caret

October 08, 2017
Tweet

More Decks by mt_caret

Other Decks in Programming

Transcript

  1. Nix(パッケージマネージャ) $ nix-env -i hello #インストール $ hello $ nix-env

    -e hello #アンインストール $ nix-env --rollback $ nix-env --list-generations
  2. Nix(言語)の文法 let x = 4; # 変数束縛(宣言/初期化) f = num:

    num + 1; # 関数 in f x # 関数適用(呼び出し)
  3. Nix(言語)の文法 let set = { num = 5; str =

    "World!"; }; # 他言語でいうRecord, Hash, Dict list = [ 1 "hoge" 4 { foo = "bar"; }]; f = { num, str }: "Hello, ${str}"; in [ set.num (head list) (f set) ] 評価すると => [ 5 1 "Hello, World!" ]
  4. 純粋関数型 - 関数の戻り値は引数から一意に定まる - 副作用を持たない let sum = 0; function

    calc_sum(num) { sum += num; return sum; } console.log(calc_sum(5)); //=> 5 console.log(calc_sum(5)); //=> 10 console.log(calc_sum(5)); //=> 15
  5. 遅延評価 式の評価(計算)を行う時、式の結果を利用 するまで評価(計算)をしない ex: f (1 + 2)の場合はf (1 +

    2)を必要とする (例えば出力等で)まで1 + 2の計算も、fへ の適用も行われない ex: let x = 5 / 0; in “this works!”
  6. Nix(言語)の文法もうすこし let set = { a = "a"; b =

    { c = "c"; }; }; abc = with set; [ a b (with b; c) ]; in abc 評価すると => [ "a" { c = "c"; } "c" ]
  7. Derivation(パッケージ)とRealization Derivationは特定のattributeを持ったSet nix-repl> d = derivation { name = “myname”;

    builder = “mybuilder”; system = “mysystem”; } nix-repl> d /nix/store/9r1...3yn-hoge.drv => /nix/store/9r1...3yn-hoge ex. $ ppsh < /nix/store/9r1...3yn-hoge.drv
  8. 例: GNU HelloのNix式 { stdenv, fetchurl }: stdenv.mkDerivation rec {

    name = "hello-2.10"; src = fetchurl { url = "mirror://gnu/hello/${name}.tar.gz"; sha256 = "0ssi1wpaf7plaswqqjwigppsg5fyh99vdlb9kzl7c9lng89ndq1i"; }; doCheck = true; meta = { description = "A program that produces a familiar, friendly greeting"; longDescription = '' GNU Hello is a program that prints "Hello, world!" when you run it. It is fully customizable. ''; homepage = http://www.gnu.org/software/hello/manual/; license = stdenv.lib.licenses.gpl3Plus; maintainers = [ stdenv.lib.maintainers.eelco ]; platforms = stdenv.lib.platforms.all; }; }
  9. Nixpkgs (https://github.com/NixOS/nixpkgs) 例のようなNix式が大量に集まったレポジトリ パッケージの数は > 8000 ex: $ nix-instantiate --eval

    --expr "with builtins; length (attrNames (import <nixpkgs> {}))" 普段使うソフトウェアは大体ある (chromium, vlc, libreoffice, etc.)
  10. NixOSのコンフィグ hardware = { cpu.intel.updateMicrocode = true; opengl.extraPackages = with

    pkgs; [ vaapiIntel ]; trackpoint = { enable = true; emulateWheel = true; speed = 250; sensitivity = 140; }; pulseaudio.enable = true; };
  11. NixOSのコンフィグ networking = { hostName = "dn"; networkmanager.enable = true;

    extraHosts = '' #0.0.0.0 twitter.com 0.0.0.0 tweetdeck.twitter.com ''; };
  12. NixOSのコンフィグ virtualisation = { libvirtd = { enable = true;

    enableKVM = true; }; docker.enable = true; lxd.enable = true; };
  13. NixOSのコンフィグ services.xserver = { autorun = true; defaultDepth = 24;

    enable = true; videoDrivers = [ "intel" ]; displayManager = { lightdm = { enable = true; background = "${background}"; }; }; windowManager = { default = "xmonad"; xmonad = { enable = true; enableContribAndExtras = true; }; }; ...
  14. NixOSのコンフィグ users = { extraUsers.delta = { isNormalUser = true;

    uid = 1000; extraGroups = [ "wheel" "networkmanager" "libvirtd" "docker" "lxd" ]; hashedPassword = "..."; }; defaultUserShell = "${pkgs.zsh}/bin/zsh"; mutableUsers = false; };
  15. 参考文献 - https://nixos.org/nixpkgs/manual/ - https://nixos.org/nix/manual/ - https://learnxinyminutes.com/docs/nix/ - https://nixos.org/nixos/nix-pills/index.html -

    https://github.com/NixOS/nixpkgs/issues/25371 - http://blog.timsears.com/posts/morenix.html - https://github.com/NixOS/nixpkgs/blob/master/pkgs/applications/misc/hello/default.nix - https://github.com/Tokyo-NixOS/presentations - https://github.com/NixOS/nixpkgs/blob/master/pkgs/build-support/trivial-builders.nix - https://github.com/NixOS/nixpkgs/blob/master/pkgs/stdenv/generic/setup.sh - https://nixos.org/nixops/manual/ - https://www.youtube.com/watch?v=w1radFbG1GU