gpt4 book ai didi

nix - nix 路径类型何时进入 nix 存储,何时不进入?

转载 作者:行者123 更新时间:2023-12-04 00:41:13 25 4
gpt4 key购买 nike

我过去注意到在 nix , 一个 ./myfile.txt路径类型似乎

  • 有时评估为 /home/myuser/mydir/myfile.txt , 和
  • 有时到 /nix/store/55j24v9qwdarikv7kd3lc0pvxdr9r2y8-myfile.txt .

  • 我想确切地了解哪种情况发生的时间。

    这对于包含任何形式的 secret 信息的文件尤其重要,如 /nix/store 中的所有文件。系统上的所有用户都可读。

    (使用 nixops 时,有一个特殊的“键”功能,请参阅手册中的 Managing keys 部分,但我认为在 nix 中何时以及如何进行路径到存储路径的复制仍然很重要本身。)

    最佳答案

    用户 clever#nixos IRC channel 解释:

    当它发生时

    扩展到/nix/store/...发生当您使用 ${} 内的路径时字符串插值 ,例如 mystring = "cat ${./myfile.txt} .

    使用 toString 时不会发生这种情况功能 ,例如toString ./myfile.txt不会为您提供指向 /nix/store 的路径.

    例如:

    toString ./notes.txt == "/home/clever/apps/nixos-installer/installer-gui/notes.txt"
    "${./notes.txt}" == "/nix/store/55j24v9qwdarikv7kd3lc0pvxdr9r2y8-notes.txt"

    它是如何发生的
    55j24v9qwdarikv7kd3lc0pvxdr9r2y8哈希部分取自 ./path 引用的文件的内容,以便在文件更改时它会更改,并且依赖它的东西可以相应地重建。

    将文件复制到 /nix/store发生在 nix-instantiate ; nix 表达式的求值仍然是纯函数式的(在求值时不会发生复制),但实例化(“构建”)不是。

    为了使这成为可能, nix 中的每个字符串有一个“上下文”来跟踪字符串所依赖的内容(实际上是 .drv 后面的路径列表)。

    例如,字符串 "/nix/store/rkvwvi007k7w8lp4cc0n10yhlz5xjfmk-hello-2.10"来自 GNU hello包有一些不可见的状态,这表示它取决于 hello推导。如果该字符串最终作为 stdenv.mkDerivation 的输入,新生成的推导将“神奇地”依赖于 hello正在构建的包。

    即使您通过 builtins.substring 弄乱了字符串,这仍然有效。 .见 this code关于如何在第 1653 行提取较长字符串的上下文,并将其用作第 1657 行中子字符串的上下文的 nix。

    您可以使用 builtins.unsafeDiscardStringContext 摆脱字符串的依赖上下文。 .

    它发生在 nix代码
    ${}插值使用 coerceToString ,其中有 bool copyToStore默认为 true 的参数:

    /* String coercion.  Converts strings, paths and derivations to a
    string. If `coerceMore' is set, also converts nulls, integers,
    booleans and lists to a string. If `copyToStore' is set,
    referenced paths are copied to the Nix store as a side effect. */
    string coerceToString(const Pos & pos, Value & v, PathSet & context,
    bool coerceMore = false, bool copyToStore = true);

    已实现 here , 并且检查插值的东西是 ./path , 并复制到 /nix/store ,正在发生 just below :

    if (v.type == tPath) {
    Path path(canonPath(v.path));
    return copyToStore ? copyPathToStore(context, path) : path;
    }
    toString prim_toString 实现, 它通过 false对于 copyToStore争论:

    /* Convert the argument to a string.  Paths are *not* copied to the
    store, so `toString /foo/bar' yields `"/foo/bar"', not
    `"/nix/store/whatever..."'. */
    static void prim_toString(EvalState & state, const Pos & pos, Value * * args, Value & v)
    {
    PathSet context;
    string s = state.coerceToString(pos, *args[0], context, true, false);
    mkString(v, s, context);
    }

    关于nix - nix 路径类型何时进入 nix 存储,何时不进入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43850371/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com