æè¿ãããã±ã¼ã¸ç®¡çãOSã®è¨å®ã宣è¨çã«ããããã¨ããæ°æã¡ã湧ãã¦ãã¦Nixã«å ¥éãããã¥ã¡ã³ããèªãã§ãããããã¯ããµã³ãã«ã³ã¼ãã«ä½ãæ¸ãã¦ããã®ãããããªãã¨ãã¾ãã¢ããã¼ã·ã§ã³ããããªãã ã¾ã Nixã«ç»å ´ããæ¦å¿µãã»ã¨ãã©ã«ãã¼ã§ãã¦ããªãããå ã«Nixè¨èªããåå¼·ãããã¨ã«ããã å¤ãã®ããã¥ã¡ã³ãã§Nix language basicsãå§ãããã¦ããã®ã§ãããèªãã§ã¿ããã¨ã«ããã
introduction
Nixè¨èªã¯ãæ¢åã®ãã¡ã¤ã«ã®å 容ãæ°ãããã¡ã¤ã«ãå°åºããã®ã«ã©ã®ããã«ä½¿ãããããç°¡åã«è¨è¿°ã§ããããè¨è¨ããã¦ããã
Nixè¨èªã§æ¸ããããã¡ã¤ã«ã¯é常ã«é£è§£ã«è¦ããããããã¯Nixè¨èªã¯ã½ããã¦ã§ã¢ã®æ§ç¯ã¨ããè¤éãªåé¡ã解決ããã®ã«ä½¿ããã¦ããããã§ãããNixè¨èªèªä½ã«ã¯èªå¨ã«çµã¿åãããããããããªã³ã³ã»ãããåå¨ããã®ã¿ã ã Nixè¨èªã®ãã¡ã¤ã«ã«è¦ãããè¤éãã¯Nixè¨èªããæ¥ãã®ã§ã¯ãªãããã®ç¨éããæ¥ããã®ã§ããã
How to run the examples?
Nixè¨èªã®ã³ã¼ãã®ä¸é¨åãNixå¼(Nix expression)ã¨å¼ã¶
Nixå¼ãè©ä¾¡ããã¨å¾ãããå¤ãNixå¤(Nix value)ã¨å¼ã¶
.nix
ãã¡ã¤ã«ã®ä¸èº«ã¯Nixå¼ã§ãã
Nixå¼ãè©ä¾¡ããã¨ããã®ã¯ãNixå¼ãè¨èªã®ã«ã¼ã«ã«åã£ã¦Nixå¤ã«å¤åããããã¨ãè¨ãã
Interactive evaluation
REPLã¯nix repl
ã§èµ·åã§ããã
Nixè¨èªã¯é
延è©ä¾¡ãæ¡ç¨ãã¦ãããããã®REPLãããã©ã«ãã§ã¯å¿
è¦ã«ãªãã¾ã§å¼ãè©ä¾¡ããªãã
å¼ãæå¾ã¾ã§è©ä¾¡ãã¦å¾ãããå¤ãè¦ããå ´åã:p expression
ã¨ããã°ããã
nix-repl> { a.b.c = 1; } { a = { ... }; } nix-repl> :p { a.b.c = 1; } { a = { b = { c = 1; }; }; }
:q
ã§REPLãçµäºããã
Evaluating Nix files
.nix
ãã¡ã¤ã«ã«æ¸ãè¾¼ãã å¼ãè©ä¾¡ããã«ã¯nix-instantiate --eval example.nix
ã¨ããã
nix-instantiate
ã¯æ¬æ¥ã¯Nixå¼ããstore derivationsã¨å¼ã°ãããã®ãçæããã³ãã³ãã
store derivationsã¯ç°¡åã«è¨ãã¨ãå¼æ°ãªã©ãå³å¯ã«æå®ããããã«ãæé ã®ã¬ã·ãã®ããã ããä»åã¯Nixè¨èªã®ç解ã«éä¸ãããããå¾åãã«ããã
--eval
ãªãã·ã§ã³ãã¤ããã¨ãã ãã¡ã¤ã«ã®ä¸ã®Nixå¼ãè©ä¾¡ãã¦å¾ãããNixå¤ãæ¨æºåºåã«åºãã ãã§ãstore derivationsã®instantiation(ãããç¾æç¹ã§ã¯ä¸æã®æ¦å¿µ)ã¾ã§ã¯ãããªãã
nix-instantiate
ã«ãã¡ã¤ã«åã渡ããªãå ´åã¯default.nix
ã¨ãããã¡ã¤ã«ãèªã¿è¾¼ããã¨ããã
nix-instantiate
ãå¿
è¦ã«ãªãã¾ã§å¼ãè©ä¾¡ãã¦ãããªãã®ã§ãå®å
¨ã«è©ä¾¡ãããNixå¤ãè¦ããå ´åã¯--strict
ãªãã·ã§ã³ãä»ããã
$ echo "{ a.b.c = 1; }" > file.nix $ nix-instantiate --eval file.nix { a = <CODE>; } $ nix-instantiate --eval --strict file.nix { a = { b = { c = 1; }; }; }
Names and values
Nixè¨èªã«ãããå¤ã«ã¯ããªããã£ããªãã¼ã¿åããªã¹ããattribute setsãé¢æ°ãåå¨ããã
attribute setsã¯ååã¨å¤ã®ãã¢ãéãããã®ã§ãJSONã¨ä¼¼ã¦ããã ãã ããJSONã¨éã£ã¦é¢æ°ãå¤ã¨ãã¦æã¤ãã¨ãã§ããã ãã¨ãã°ã以ä¸ã¯ããªããã£ãåããªã¹ããattribute setsãå¤ã¨ãã¦æã¤attribute setsã
{ string = "hello"; integer = 1; float = 3.141; bool = true; null = null; list = [ 1 "two" false ]; attribute-set = { a = "hello"; b = 2; c = 2.718; d = false; }; }
name = value;
ã¨ããå½¢ã§å¤ã«ååãå²ãå½ã¦ãããã
ä»ã®è¨èªã§è¨ãã¨ããã®å¤æ°ã®æç¸ã ã¨æãããããããã¥ã¡ã³ãã§ã¯Variableã¨ããè¨èãç»å ´ããNameã¨æ¸ãã¦ããã
ã¾ããè¦ã¦ãããã¨ãããªã¹ãã®è¦ç´ ã¯ã¹ãã¼ã¹åºåãã
Recursive attribute set rec { ... }
å
é ã«rec
ãä»ãã¦å®£è¨ãããattribute setsã®ä¸ã§ã¯ãå¼ã®ä¸ã§ããã¾ã§ã«å¤ã«ä»ããååã使ããã¨ãã§ããã
rec { one = 1; two = one + 1; three = two + 1; }
nix-repl> :p rec { one = 1; two = one + 1; three = two + 1; } { one = 1; three = 3; two = 2; }
attribute setã®è¦ç´ ã¯å¥½ããªé ã«å®£è¨ãã¦åé¡ãªããè©ä¾¡æã«é åºã決ã¾ãã
ããããrec
ãä»ããªãã¨ããã§ããªãããã«ããçç±ãä½ããã£ã¦ãã®å
æããã«ãªãã®ã ãããã
æ°ã«ãªãã¨ããã
let ... in ...
let
ã¨in
ã®éã§å¤ã«ååãä»ããin
ã®å¾ã«ãã®ååã使ã£ã¦çµã¿ç«ã¦ãå¼ãè©ä¾¡ããå¤ãè¿ãããã
let a = 1; in a + a
nix-repl> let a = 1; in a + a 2
Haskellã®letå¼ã¨åãã£ã½ã...ã¨æã£ããå¤ã«ååãä»ããé çªã¯å¥½ãã«ãã£ã¦ãããããã
nix-repl> let b = a + 1; a = 1; in a + b 3
let
å¼ã¨å帰çãªattribute setsã¯ã©ã®ãããªé çªã§å¤ã«ååãå²ãå½ã¦ã¦ããããä»ã®ååã=
ã®å³å´ã®å¼ã«ä½¿ã£ã¦ãããã¨ããç¹ã§ä¼¼ã¦ããã
ä¸æ¹ã§å帰çãªattribute setsã¯attribute setã¨ãã¦è©ä¾¡ãããããlet ... in ...
ããã¯(attribute setãå«ãã¦)ã©ããªå¤ã§ãè¿ããã
nix-repl> let a = c + 1; b = a + 1; c = b + 1; in a + b + c error: infinite recursion encountered at «string»:1:20: 1| let a = c + 1; b = a + 1; c = b + 1; in a + b + c | ^
循ç°åç §ã¿ãããªãã¨ããã£ã¦ã¿ããå½ç¶æãããã
let
å¼ã®ä¸ã§ä»ããååã«ã¢ã¯ã»ã¹ã§ããã®ã¯ãã®let
å¼ã®ä¸ã®ã¿ã
with ...; ...
1ã¤ã®å¼ã®ä¸ã§ç¹å®ã®attribute setã®è¦ç´ ã«ç¹°ãè¿ãã¢ã¯ã»ã¹ããéã«ä½¿ããæ§æã
let a = { x = 1; y = 2; z = 3 }; in with a; [ x y z ]
[ 1 2 3 ]
ããã¯
let a = { x = 1; y = 2; z = 3; } in [ a.x a.y a.z ]
ã¨åå¤ã
è¤æ°with
ã並ã¹ãã°è¤æ°ã®attribute setã®ä¸ã®å±æ§ãåæã«åç
§ãããã¨ãã§ãããããã
let a = { x = 1; y = 2; z = 3; }; b = { v = 4; w = 5; }; in with a; with b; [x y z v w]
[ 1 2 3 4 5 ]
attribute setã®ä¸ã®ååã被ã£ã¦ããã¨ãã¯å¾ã«with
ã§æå®ããæ¹ã®å¤ãåªå
ãããï¼
let a = { x = 1; y = 2; z = 3; }; b = { x = 4; y = 5; }; in with a; with b; [ x y z ]
[ 4 5 3 ]
便å©ã ã¨æãåé¢ä½¿ããããã¨ä¿å®æ§ãæªåããããªæ°ãããã
inherit ...
JavaScriptã®{ x, y }
ã®ããã«ãæ¢ã«ååãã¤ãã¦ãå¤ãattribute setã«å ããããã®ãã¼ã¯ã¼ãã
let x = 1; y = 2; in { inherit x y; }
ããã¯
let x = 1; y = 2; in { x = 1; y = 2; }
ã¨å ¨ãåãã
with
ã¨ã¯éã£ã¦inherit
ã§æå®ããååã¨åãååãããä¸åº¦æç¸ãããã¨ããã¨ã¨ã©ã¼ã«ãªãã
let x = 1; y = 2; in { inherit x y; x = 3; }
ããã¯with
ã¨éã£ã¦ãããªãã¨ã許容ãã¦ãå½¹ã«ãã¤ã±ã¼ã¹ããããåå¨ããªãããinherit
ã®æ©è½ã¨ãã¦ã¨ã©ã¼ã«ããã¦ããã®ãããããã¯x = 1; y = 2; x = 3;
ã¨å±éããã¦ã1ã¤ã®attribute setã®ä¸ã§åãååã¯2度使ããªãã¨ããã«ã¼ã«ã«ãã£ã¦ã¨ã©ã¼ã«ãªã£ã¦ããã®ã ãããï¼
with
ã¨åãããæ¢åã®attribute setããç¹å®ã®ååã ããåãåºãã¦æ°ããattribute setã«å ãããã¨ãã§ããã
let a = { x = 1; y = 2; z = 3; }; in { inherit (a) x y; }
{ x = 1; y = 2; }
2ã¤ä»¥ä¸ã®attribute setãinheritã§åãååã®å¤ãåãåºããã¨ããã¨ãã¯ãã¨ã©ã¼ã«ãªãã
let a = { x = 1; y = 2; }; b = { x = 1; z = 3; }; in { inherit (a) x y; inherit (b) z; }
String interpolation ${ ... }
大ä½Scalaã¨åããããã
æããScalaã®ããã«ã«ã¹ã¿ã ããè£å®åã¯ä½ããªãã®ãå
é ã«s
ãä»ããå¿
è¦ãç¡ãã®ã¨ãåå1ã¤ããæãå¼ãåãè¾¼ãæã§ã{}
ãå¿
é ã«ãªã£ãããããã
let name = "Nix"; in "hello ${name}
"hello Nix"
å½ç¶ãstring
å¤ãæååã«å¤æã§ããåã®å¤ããåãè¾¼ããªãã®ã ãããªãã¨integer
åããæååã«å¤æã§ããåã¨ãã¦æ±ããã¦ããªããããã
ä¸ä½string
以å¤ã«ã©ã®åãåãè¾¼ãããã ããã
æååè£å®åã®ãã¹ããã§ãããå¯èªæ§ã¯æªããã
ã¡ãªã¿ã«ãNixè¨èªã®ç¨éã®é½åä¸ãNixè¨èªã®ã³ã¼ãã®ä¸ã«ã¤ã³ã©ã¤ã³ã§ã·ã§ã«ã¹ã¯ãªããæ¸ããå ´åã«ãã®ã¹ã¯ãªããã®ä¸ã§$var
ã®ããã«"$"
ãæååã«ç»å ´ãããã¨ã¯æ®éã«ããããã ã
åå1ã¤ããæãå¼ãåãè¾¼ãå ´åã{}
ãå¿
é ãªã®ã¯ããããäºæ
ãããã®ãããããªãã
File system paths
stringã¨ã¯å¥ã«ãã¹ã表è¨ããåããããããã
絶対ãã¹ãè¨è¿°ããæ¹æ³ã¨ç¸å¯¾ãã¹ãè¨è¿°ããæ¹æ³ãããã/
ããå§ããã¨çµ¶å¯¾ãã¹ãè¨è¿°ã§ããã
nix-repl> /foo/bar /foo/bar
/
以å¤ããå§ããã¨ç¸å¯¾ãã¹ã¨ãã¦è©ä¾¡ãããã
ç¸å¯¾ãã¹ã¯å¿
ã/
ãå«ã¾ãªããã°ãªããªãã¨ããã«ã¼ã«ãããã
.
ã¯ããã¾ã§ã®ãã¹ã®ãã£ã¬ã¯ããªãæããä¾ã¨ãã¦foo/.
ã¨æ¸ãã¨(ä»®ã«ããæ¸ããããã¡ã¤ã«ã/current/directory
ã¨ãããã¹ã«ç½®ãã¦ããå ´åã¯)/current/directory/foo
ã¨ãããã¹ã«ãªãã
ãããã®2ã¤ã®ã«ã¼ã«ããNixè¨èªã§ã¯ãã¡ã¤ã«ãç½®ããã¦ãããã£ã¬ã¯ããªãæãéã«./.
ã¨ããè¨è¿°ã使ãã
..
ã§è¦ªã®ãã£ã¬ã¯ããªãæããã
ã¾ãããã¹ã¯æååè£å®ã§åãè¾¼ããã¨ãã§ããã
Lookup paths
ãã®ãã¼ãã¯æ¸ãã¦ããå 容ããã¾ãç解ã§ããªãã£ãã®ã§ãä¸åº¦é£ã°ããå¾ã§ãã®Lookup pathsãåç»å ´ããæã«æ»ã£ã¦ããã ããã§ãåãããªãã®ã§Nix Reference Manualãèªã¿ã«ãã£ããããã§ãç解ãããã«è³ããªãã£ãã®ã§ãããã£ãã¨ããã¾ã§ãæ¸ãã¦ããã
Lookup pathsã¨ã¯<nixpkgs>
ã<nixpkgs/lib>
ã®ãããª<>
ã§å²ã¾ãããã¹ã§ããã<nixpkgs>
ãè±ç³ããã¨builtins.findFile builtins.nixPath "nixpkgs"
ã«ãªãã
builtins
ã¨ããã®ã¯ããããå¾ã«åºã¦ããã®ã ãNixè¨èªã®ã¤ã³ã¿ããªã¿ã«å®è£
ãããã©ã¤ãã©ãªã§ãã³ã¼ãä¸ã§åç
§ããã¨æ§ã
ãªé¢æ°ãè¦ç´ ã¨ãã¦æã¤attribute setã«ãªã£ã¦ããã
nix-repl> builtins { abort = «primop abort»; add = «primop add»; addDrvOutputDependencies = «primop addDrvOutputDependencies»; addErrorContext = «primop addErrorContext»; all = «primop all»; any = «primop any»; appendContext = «primop appendContext»; # ... (ã¨ã¦ãè¦ç´ æ°ãå¤ãã®ã§çç¥) }
builtins.findFile
ã¯ãããªé¢æ°ã®å
ã®1ã¤ã ã
builtins.findFile builtins.nixPath "nixpkgs"
ã¨ããå¼ã§ã¯builtins.nixPath
ã¨"nixpkgs"
ã¨ãã2ã¤ã®å¼æ°ãä¸ãããã¦ããã
builtins.findFile
ã®æåã説æããã«ããã£ã¦builtins.nixPath
ãä½ãªã®ããå
ã«ç¥ã£ã¦ãããæ¹ãããããããã®ã§ãã¡ããå
ã«è¦ãã
ç§ã®ç°å¢ã§ã¯ãã®ãããªattribute setã«ãªã£ã¦ããã
nix-repl> :p builtins.nixPath [ { path = "/home/pkino/.nix-defexpr/channels"; prefix = ""; } { path = "flake:nixpkgs"; prefix = "nixpkgs"; } ]
ãã®ããã«builtins.findFile
ã®ç¬¬1å¼æ°ã«ã¯path
ã¨prefix
ã¨ããå±æ§ãæã¤attribute setã®ãªã¹ãã渡ãããã
builtins.findFile
ã¯ãã®ä¸ã§prefixã第2å¼æ°ã«ããããããã®ãè¦ã¤ããã¨ããã®attribute setã®pathå±æ§ãè¿ãã
ãªã®ã§ãbuiltins.findFile builtins.nixPath "nixpkgs"
ã¨ããã¨"flake:nixpkgs"
ãè¿ã£ã¦ããã
ãããå®éã«REPLã§<nixpkgs>
ãè©ä¾¡ãããã¨ä»¥ä¸ã®ããã«å±éãããã
nix-repl> <nixpkgs> /nix/store/g9bw874w67bfsvg93szwnxxjxp4lphby-source
ã©ããattributeã®path
å±æ§ãããã±ã¼ã¸åã ã¨ããã®ããã±ã¼ã¸ããã¦ã³ãã¼ããã¦ãã®ãã£ã¬ã¯ããªã®çµ¶å¯¾ãã¹ãè¿ãã¦ãããããªã®ã ãããã®ãããããªãã¡ã¬ã³ã¹ããã¥ã¢ã«ã§ã¯è©³ããã¯èª¬æããã¦ããããå¾ã¯å®éã«builtins.findFile
ã®ã³ã¼ããèªã¿ã«è¡ããããªãããã ãç¾ç¶ã§ããã¾ã§ã®ã¢ããã¼ã·ã§ã³ãç¡ãã®ã§å
ã«é²ããã¨ã«ããã
ãªããbuiltins.findFile builtins.nixPath "nixpkgs/lib/network"
ã®ããã«ç¬¬2å¼æ°ã«/
ãå«ãæååã渡ãã¨æåã®/
ã¾ã§ã®æååãprefix
å±æ§ã¨ã®ãããã³ã°ã«ä½¿ãããè¿ãå¤ã¯path
ã®æ«å°¾ã«æ®ãã®ãã¹ããã£ã¤ãããã®ã«ãªãã
nix-repl> builtins.findFile builtins.nixPath "nixpkgs/lib/network" /nix/store/g9bw874w67bfsvg93szwnxxjxp4lphby-source/lib/network
"nixpkgs"
ããããã³ã°ã«ä½¿ãããè¿ãå¤ã®æå¾ã«"/lib/network"
ã追å ããã¦ããã®ããããã ããã
ããã¾ã§é·ã説æããããã©ãããLookup pathsã¯åç¾æ§ããªãä¸ç´ã§ãããã(使ããã¦ãã¾ã£ã¦ã¯ããã®ã ã)ãã¾ã使ããªãæ¹ããããããã
Indented strings
''
ã§æååãå²ãã¨è¤æ°è¡ã®æååãæ¸ããã
nix-repl> '' foo bar baz '' "foo\nbar\nbaz\n"
ä¸çªã¹ãã¼ã¹ãå°ãªãè¡ãã¤ã³ãã³ãã®åºæºã«ãããã
nix-repl> '' foo bar baz '' "foo\n bar\n baz\n" nix-repl> '' foo bar baz '' " foo\n bar\nbaz\n"
Functions
é¢æ°ã¯arg: expression
ã¨ããå½¢ã§è¡¨è¨ãããã
å®éã®ããã¥ã¡ã³ãã§ã¯æ¬¡ã®ãã¼ãã§èª¬æããã¦ããããé¢æ°ã®é©ç¨ã¯Haskellã¨åæ§functionName arg
ã¨ããå½¢ã§è¡ãã
nix-repl> f = x: x + 1 nix-repl> f 1 2
Nixã§ã¯1å¼æ°é¢æ°ã®ã¿ãåå¨ããããattribute setãå¼æ°ã«ã¨ããã¨ã§è¤æ°ã®å¼æ°ãæ±ããã
nix-repl> f = { x, y }: x + y nix-repl> a = { x = 1; y = 2; } nix-repl> f a 3
ãªãããã®é¢æ°ãä½åãªååãå«ãattribute setã«å¯¾ãé©ç¨ãããã¨ããã¨ã¨ã©ã¼ã«ãªãã
nix-repl> a = { x = 1; y = 2; z = 3; } nix-repl> f a error: ⦠from call site at «string»:1:1: 1| f a | ^ error: function 'anonymous lambda' called with unexpected argument 'z' at «string»:1:2: 1| { x, y }: x + y | ^ Did you mean one of x or y?
ä½åãªååãå«ãattribute setãåãå ¥ããã«ã¯ã以ä¸ã®ããã«ããã
nix-repl> f = { x, y, ... }: x + y nix-repl> a = { x = 1; y = 2; z = 3; } nix-repl> f a 3
HaskellãScalaã®ãã¿ã¼ã³ãããã«ãããasãã¿ã¼ã³ã®ããã«ãå¼æ°ã«åãåãattribute setèªä½ã«ååãä»ãããã¨ãã§ããã
nix-repl> f = args@{ x, y, ... }: x + y + args.z nix-repl> a = { x = 1; y = 2; z = 3; } nix-repl> f a 6
ã©ã®å¼æ°ãæ±ãããã¦ããã®ããããã¥ããä¿å®æ§ãæªåãããã ããããã±ã¼ã¸ã®è¨å®ã®ããã«é常ã«å¤ãã®ãã©ã¡ã¼ã¿ãåãåããã¨ã«ãªã£ããããããããå¾ãªããã ãããªã¨ããæ°ãããã å¾ç½®ã§ãããã
nix-repl> f = { x, y, ... }@args: x + y + args.z
ããã©ã«ãå¼æ°ã使ããã
nix-repl> f = { x, y ? 0 }: x + y nix-repl> a = { x = 1; } nix-repl> f a 1
å¿åã®é¢æ°ã¯lambdaã¨å¼ã°ããã
Calling functions
以ä¸ã®ããã«ãé¢æ°ã®é©ç¨ãæ£ããè¡ãããã«ãã£ãã§å²ã¾ãªããã°ãªããªãã±ã¼ã¹ãããã
nix-repl> let f = x: x + 1; a = 1; in [ (f a) ] [ 2 ] nix-repl> let f = x: x + 1; a = 1; in [ f a ] [ «lambda f @ «string»:2:7» 1 ]
ãã®ã±ã¼ã¹ã§ã¯ãªã¹ãã®è¦ç´ ã¯ã¹ãã¼ã¹åºåãã§è¨è¿°ããããããä¸ã¯f a
ã®è¿ãå¤ãè¦ç´ ã«æã¤ãªã¹ããä½æãããããä¸ã¯f
ã¨a
ãè¦ç´ ã«æã¤ãªã¹ãã«ãªã£ã¦ãã¾ãã
Multiple arguments
é¢æ°ã®è¿ãå¤ãé¢æ°ã«ãããã¨ã«ããã«ãªã¼åãã§ããã
nix-repl> f = x: y: x + y nix-repl> f 1 2 3 nix-repl> f2 = f 1 nix-repl> f2 2 3
x: y: x + y
ã¯x: (y: x + y)
ã¨è©ä¾¡ããã¦ãããã¨ããããã
Function libraries
Nixè¨èªã§åºã使ãããã©ã¤ãã©ãªã¨ãã¦builtins
ã¨pkgs.lib
ãæããããã
builtins
ã¯ãã®åã®éãNixè¨èªã®ã¤ã³ã¿ããªã¿ã«C++ã§å®è£
ããã¦ããé¢æ°ã§ãããã«ãªãã¡ã¬ã³ã¹ããã¥ã¢ã«ãããã
pkgs.lib
ã¯nixpkgs
ãªãã¸ããªã«lib
ã¨å¼ã°ããattribute setãããã便å©ãªé¢æ°ãæä¾ãã¦ããã
ãã¡ãã¯Nixè¨èªã§å®è£
ããã¦ããã
æããããã®ãã¨ã ã¨æãããã
ç¹å®ã®Nixãã¡ã¤ã«ã«å®ç¾©ããã¦ããé¢æ°ãå¼ã³åºããããªããã¾ããã®nixãã¡ã¤ã«ã«æ¸ãããå¼ãè©ä¾¡ãå¤ãå¾ãå¿
è¦ãããã
ãã¨ãã°ã./test/lib.nix
ã«ä»¥ä¸ã®å
容ãæ¸ãè¾¼ãã
{ a = 1; }
import
ã¨ãããã¼ã¯ã¼ãã¯ãã¹åã®å¤ãåãåãé¢æ°ã®ããã«ãªã£ã¦ãã(ãããã¯æ¬å½ã«é¢æ°ãªã®ãããããªã)ãimport
ã«nixãã¡ã¤ã«ã®ãã¹ã渡ããã¨ã§ãã®ãã¡ã¤ã«ã«æ¸ãããå¼ãè©ä¾¡ã§ããã
nix-repl> import ./test/lib.nix { a = 1; }
ä»ã®nixãã¡ã¤ã«ãè¿ãattribute setã®è¦ç´ ã§ããå¤ãæ±ããããªãã以ä¸ã®ããã«import
ãè¿ããattribute setã®è¦ç´ ããã®ã¾ã¾åç
§ããããimport
ãè¿ããattribute setã«ååãä»ãããã¨ã«ãªãã
大æµã¯å¾è
ã§ãããã¨ã«ãªãã¨æãããã
nix-repl> (import ./test/lib.nix).a 1 nix-repl> test-lib = import ./test/lib.nix nix-repl> test-lib.a 1
ã¾ããimport
ã«ãã£ã¬ã¯ããªã®ååã渡ãã¨ããã®ãã£ã¬ã¯ããªã«ããdefault.nix
ã¨ããååã®ãã¡ã¤ã«ãæ¢ãããã
./test/default.nix
ã¨ãããã¡ã¤ã«ã«ä»¥ä¸ã®å
容ãæ¸ãè¾¼ãã
{ add = x: y: x + y; }
以ä¸ã¯ãã®ãã¡ã¤ã«ããã£ã¬ã¯ããªãæå®ãå©ç¨ãã¦ããä¾ã
nix-repl> test = import ./test nix-repl> test.add 1 2 3
ãªã<nixpkgs>
ãè©ä¾¡ãã¦å¾ããããã£ã¬ã¯ããªã®default.nix
ãè¿ãå¤ã¯é¢æ°ã«ãªã£ã¦ãããæ
£ä¾çã«ç©ºã®attribute setã渡ãã¦å©ç¨ããã¦ããã
nix-repl> pkgs = import <nixpkgs> {} nix-repl> pkgs.lib.strings.toUpper "foo" "FOO"
pkgs
ãpkgs.lib
ã¯æ
£ä¾ã¨ãã¦é¢æ°ã®å¼æ°ã¨ãã¦æ¸¡ããããã¨ãããããã
{ pkgs, lib, ... }: # ...
ãã®å ´åã¯å¼ã³åºãå´ã§nixpkgs
ãimportã渡ããä¾åæ§æ³¨å
¥ã®ãããªæä½ãè¡ããã¨ã«ãªãã
Impurities
Nixè¨èªã§ã¯ããã«ãã®ä¸ã§ç¹å®ã®ãã¡ã¤ã«ãåç §ãããã¨ãããã ãã®ãããªãã¡ã¤ã«ããã«ãæã®å ¥åã¨ãã¦æ¸¡ãæ¹æ³ã¯ããã¡ã¤ã«ãã¹ãè¨è¿°ããããFetcherã¨å¼ã°ããå°ç¨ã®é¢æ°ã使ããã®ã©ã¡ããã«ãªãã
Paths
ããã¡ã¤ã«ãã¹ãè¨è¿°ãããæ¹ã®æ¹æ³ã ãã¡ã¤ã«ã·ã¹ãã ãã¹ãæååè£å®åã®ä¸ã§ä½¿ãããã¨ããã®ãã¡ã¤ã«ã¯Nix storeã¨ããNixãããã±ã¼ã¸ç®¡çã«ä½¿ã£ã¦ãããã£ã¬ã¯ããªã«ã³ãã¼ããããã®ãã¹ãè¿ãããã
nix-repl> "${./test/lib.nix}" "/nix/store/41yix9s3lf3vsrmxk59n6j0i1c63ymj9-lib.nix"
æååè£å®åã«åãè¾¼ãã å¤ã¯æååã¨ãã¦è¡¨ç¾ã§ãããã®ã«è©ä¾¡ãããªããã°ãªããªãã
ãã¡ã¤ã«ã·ã¹ãã ãã¹ã®å ´åã¯ã対å¿ããNixã¹ãã¢ã®ãã¡ã¤ã«ã®ãã¹ã¨ãã¦è©ä¾¡ãããã
Nixã¹ãã¢ã®ãã¡ã¤ã«ãã¹ã¯/nix/store/<hash>-<name>
ã¨ããå½¢å¼ã«ãªã£ã¦ããã<hash>
ã®é¨åã«ã¯ãã¡ã¤ã«å
容ã®ããã·ã¥ãã<name>
ã«ã¯å
ã®ãã¡ã¤ã«åãå
¥ãã
Nixè¨èªèªä½ããã¯è±ç·ããããNixã¯ãã«ãã«ä½¿ããã¡ã¤ã«ã®ä¸èº«ãããã·ã¥ã¨ãã¦è¨é²ãããã¨ã§ããã«ãã®åç¾æ§ãæ
ä¿ãã¦ããããã ã
ãã£ã¬ã¯ããªã®ãã¹ãåãè¾¼ãã å ´åããå¯ä½ç¨ã¨ãã¦ãã£ã¬ã¯ããªå ¨ä½ãNix storeã«ã³ãã¼ããããã®ãã£ã¬ã¯ããªã®Nix storeãã¹ãè¿ã£ã¦ããã
Fetchers
ãã«ãæã®å
¥åã«ä½¿ããããã¡ã¤ã«ã¯ãã¡ã¤ã«ã·ã¹ãã ããã ãã§ãªãããããã¯ã¼ã¯ããåå¾ãã¦ãããã
ãã®ããã®é¢æ°ãbuiltins
ããã±ã¼ã¸ã«ç¨æããã¦ããã
builtins.fetchurl
builtins.fetchTarball
builtins.fetchGit
builtins.fetchClosure
ãããã®é¢æ°ã«ããåå¾ããããã¡ã¤ã«ã¯ãã¯ãNix storeã«ä¿åããããã®ãã¹ãé¢æ°ã®è¿ãå¤ã¨ãã¦è¿ãããã
Derivations
(å ã«æ¸ãããã¨ã®ç¹°ãè¿ãã«ãªãããDerivationã¨ã¯ããã±ã¼ã¸ã®ãã«ãæé ã®ã¬ã·ã)
Nixè¨èªã¯Derivationsãè¨è¿°ããã®ã«ä½¿ããã Nixã¯Derivationsããææç©ãçæããã ãã«ããããææç©ã¯ä»ã®Derivationsã®å ¥åã«ä½¿ããããã¨ãããã
ææ³
å®ã¯Nix language basicsãèªãã§ããéã«ã ãã ãæãåãããããªã£ã¦ãã¦ãã¾ããVirtualBoxã«NixOSãã¤ã³ã¹ãã¼ã«ãã¦ã¡ãã£ã¨ããè¨å®ããããã¨ããããå§ãã¦ããã ã¾ã è¨è¿°ãã¦ããå 容ã«ããä½ãèµ·ããã®ãã¾ã§ã¯ç解ã§ãã¦ããªãããã©ãããæ§æã使ããã¦ãNixå¼ãã©ãããæ§æã«ãªã£ã¦ããã®ããããã¯ãããããã«ãªã£ã¦æ¥½ãããªã£ã¦ããã ãã ãã¾ã Nixã«ç»å ´ããæ¦å¿µãå ¨ç¶ç解ã§ãã¦ããªãã®ã§ã次ã¯NixOS & Flakes Bookãèªãã§ã¿ããã¨æãã NixOSã¯é£æ度ãé«ãã¨èãã¦ãã常ç¨ãã¦ãããã¯æªå®ã ããããã±ã¼ã¸ã®ç®¡çãNixã«ç§»è¡ããã¨ããã¾ã§ã¯ãã£ã¦ã¿ããã