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

Nixの仕組み: Derivation編

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on.

Nixの仕組み: Derivation編

nixパッケージマネージャとderivationについて

Avatar for comavius

comavius

May 14, 2025
Tweet

More Decks by comavius

Other Decks in Programming

Transcript

  1. お品書き - パッケージとは - Nixとは? - Nixの利用 - derivationのコンセプト -

    derivationの評価とビルド - パッケージの再現性 - キャッシュによる効率化とGC - おわりに 4
  2. Nixとは? 公式サイト(https://nixos.org/guides/how-nix-works/)曰く、 Nix is a purely functional package manager. This

    means that it treats packages like values in purely functional programming languages such as Haskell - they are built by functions that don’t have side-effects, and they never change after they have been built. Nix stores packages in the Nix store, usually the directory /nix/store, where each package has its own unique subdirectory (略) 10
  3. Nixとは? 公式サイト(https://nixos.org/guides/how-nix-works/)曰く、 Nix is a purely functional package manager. This

    means that it treats packages like values in purely functional programming languages such as Haskell - they are built by functions that don’t have side-effects, and they never change after they have been built. Nix stores packages in the Nix store, usually the directory /nix/store, where each package has its own unique subdirectory (略) - Nixは純粋関数型パッケージマネージャ 11
  4. Nixとは? 公式サイト(https://nixos.org/guides/how-nix-works/)曰く、 Nix is a purely functional package manager. This

    means that it treats packages like values in purely functional programming languages such as Haskell - they are built by functions that don’t have side-effects, and they never change after they have been built. Nix stores packages in the Nix store, usually the directory /nix/store, where each package has its own unique subdirectory (略) - Nixは純粋関数型パッケージマネージャ - 関数型言語を使ってパッケージを値のように扱う 12
  5. Nixとは? 公式サイト(https://nixos.org/guides/how-nix-works/)曰く、 Nix is a purely functional package manager. This

    means that it treats packages like values in purely functional programming languages such as Haskell - they are built by functions that don’t have side-effects, and they never change after they have been built. Nix stores packages in the Nix store, usually the directory /nix/store, where each package has its own unique subdirectory (略) - Nixは純粋関数型パッケージマネージャ - 関数型言語を使ってパッケージを値のように扱う - ビルド成果物は通常/nix/store以下に保存される 13
  6. derivationのコンセプト derivation :: { system : String , name :

    String , builder : Path | Derivation , ?args : [String] , ?outputs : [String] } -> Derivation (https://zero-to-nix.com/concepts/derivations/) 22
  7. derivationのコンセプト 例えば、helloと出力するコマンドを作る場合 ... derivation { name = "hello"; system =

    "x86_64-linux"; builder = "bash"; args = [ "-c" "mkdir $out/bin && printf "#!/bin/sh\necho hello" > $out/bin/hello && chmod +x $out/bin/hello" ] }; 23
  8. Derivationの評価とビルド - 流れ(nix eval) nix evaluator nix store(/nix/store ^1) ^1:

    /nix/storeをnix storeとして利用するのはlocal storeのみで、必ずしも/nix/storeに保存されない(S3 storeなど) nix式の評価... derivationの評価 instantiationによるstore-derivation  (/nix/store/…..hoge.drv)への保存 nix-instantiateの呼び出し 30
  9. Derivationの評価とビルド - 流れ(nix build) nix evaluator nix store(/nix/store) nix式の評価... derivationの評価

    instantiation(/nix/store/…..hoge.drvの保存) nix-instantiateの呼び出し realisationによるstore-object (/nix/store/…..hoge)のビルド nix-realiseの呼び出し 33
  10. Derivationの評価とビルド - 流れ(nix build w/ IFD) nix evaluator nix store(/nix/store)

    nix式の評価... IFDのコンテキストでのderivationの評価 IFDによるnix-realiseの呼び出し IFDによるstore-object(/nix/store/…..hoge) からの値の読み込みとevaluatorでの利用 36
  11. パッケージの再現性 「Nixとは?」で確認した公式サイト (https://nixos.org/guides/how-nix-works/)による説明の続き …where each package has its own unique

    subdirectory such as /nix/store/b6gvzjyb2pg0kjfwrjmg1vfhh54ad73z-firefox-33.1/ where b6gvzjyb2pg0… is a unique identifier for the package that captures all its dependencies (it’s a cryptographic hash of the package’s build dependency graph). This enables many powerful features. - 依存関係全体のハッシュをパッケージの名前の一部にする 40
  12. キャッシュによる効率化と GC nix evaluator nix store(/nix/store) 46 nix store(S3) ローカル

    S3(互換)サーバー 過去のrealiseのキャッシュを利用 リモートのキャッシュからビルド 成果物を取得 今回の成果物を アップロード