Nixのderivation
頻繁に使われる用語だが、実は割と指しているものが曖昧mrsekut.icon
どこにも厳密な定義が書かれていない #??
雑に言うと、Nixにおけるpackageのこと
ただし、Nixのdocsとかを読んでも分かる通り、packageとderivationという単語は明確にわけて用いられている
以下のもの(の、どれか)を指す
builtins.derivationを用いて定義した.nixのfile
store derivation
store derivation outputs
そもそも曖昧なので、技術的な厳密な話をしたい時は「derivation」という単語の使用を避けたほうが良いと思うmrsekut.icon
#WIP
https://scrive.github.io/nix-workshop/04-derivations/01-derivation-basics.html
こういうことらしい
What is the difference between a “derivation” and a “store derivation”?
“Store derivation” refers to a .drv file, “derivation” is a fuzzy word that could mean any of like three different things.ref
store derivation
.drvの拡張子のもの
これはnix-instantiateの出力
「Nixのderivation」はfuzzy word
3つってなんだ?
https://gyazo.com/fbb90b5bb61bac3c9671ed8e6448a3f3 https://stackoverflow.com/a/58243537
これの3つの丸か?
fuzzy wordのくせに跋扈しすぎだろmrsekut.icon
マジでfuzzy wordなのであればmrsekut-pからリンクを消したいなmrsekut.icon
あるいは一番右側の丸を指すということにするか
buildの出力
A derivation (see example) is a computer-friendly representation of the build recipes used for building (realize) packages. They are files with the extension .drv which are listed in the store directory, usually /nix/store. ref
あるいは「以下のものの内どれかを指す」として、それらをリンクしておくか
fuzzy wordだけど、実際その辺のものをふわっと指すために便利な気もする
builtins.derivation関数の出力のことを指す
ざっくり言うと、
Nix言語で記述されたbuild方法を記述した.nixのことを指す
Nixが利用するpackageはderivationの形式で提供、共有される
例えば、nixpkgsの中身はderivationの形式になっている
derivationをcompileすると、store derivationが生成される
故に、「derivationとはstore derivationのこと」と説明されることもある ref
用語が揺れるのは困るが、公式に明確な用語の定義が見られないので、Nix Pillsのノリに則って、今のところはこのスクボリンクのまま運用するmrsekut.icon
明確な定義を見つけられれば、Nixのderivationとstore derivationのノートをmergeするなりなんなりするかもしれない
新しくderivationを定義する
builtins.derivationを使う
3つの必須項目を指定しさえすればderivationを作れる
code:nix-repl
nix-repl> d = derivation { name = "myname"; builder = "mybuilder"; system = "mysystem"; }
nix-repl> d
«derivation /nix/store/z3hhlxbckx4g3n9sw91nnvlkjvyw754p-myname.drv»
具体例
Nixでderivationを定義して、簡単なCプログラムをbuildしてみる
derivationのattributes
builtins.derivationの返り値であるsetsの各attrを見るmrsekut.icon
builtins.attrNamesでどんなattrを持っているか確認できる
code:nix-repl
nix-repl> d = builtins.derivation {..}
nix-repl> builtins.attrNames d
"all" "builder" "drvAttrs" "drvPath" "name" "out" "outPath" "outputName" "system" "type"
この辺はbuiltins.derivationの必須3項目と同じ
name
builder
system
all
drvAttrs
builder, name, systemの基本事項
drvPath
.drvfileのpath
/nix/store/<hash-name>.drv
out
自分自身
d == d.out
outPath
output paths
nix store上のbuild path
/nix/store/<hash-name>
outputName
type
type = "derivation"というNix内で特別扱いされるfieldになっている
このsetsがderivationであることを表す
https://nixos.org/guides/nix-pills/our-first-derivation.html#idm140737320422816
Fixed Output Derivations
ってなに #??
https://comono.id/posts/2020-03-20-how-nix-instantiation-works/
https://nixos.org/guides/nix-pills/nix-store-paths.html
https://discourse.nixos.org/t/using-fixed-output-paths-for-a-derivation/6338
https://github.com/NixOS/nix/issues/2270
参考
Nix Pills 6
https://nixos.org/manual/nix/unstable/expressions/derivations.html
https://stephank.nl/p/2020-06-01-a-nix-primer-by-a-newcomer.html#:~:text=ahead%20through%20manuals.-,Derivation,-A%20special%20type