gpt4 book ai didi

nix - 编写本地 nix 包

转载 作者:行者123 更新时间:2023-12-04 19:37:24 28 4
gpt4 key购买 nike

我想运行一个安装了以下软件包的 nix-shell:

  • aspell
  • aspellDicts.en
  • 你好

  • 我不能简单地做: nix-shell -p aspell aspellDicts.en hello --pure因为这将无法正确安装 aspell 词典。 Nix 提供了一个 aspellWithDict可用于使用字典构建 aspell 的函数:
    nix-build -E 'with import <nixpkgs> {}; aspellWithDicts (d: [d.en])'

    我想将此构建的结果用作另一个本地包 (foo) 中的依赖项。这就是我目前实现这一目标的方式:

    ./pkgs/aspell-with-dicts/default.nix:
    with import <nixpkgs> {};

    aspellWithDicts (d: [d.en])

    ./pkgs/foo/default.nix:
    {stdenv, aspellWithDicts, hello}:

    stdenv.mkDerivation rec {
    name = "foo";
    buildInputs = [ aspellWithDicts hello ];
    }

    ./custom-packages.nix:
    { system ? builtins.currentSystem }:

    let
    pkgs = import <nixpkgs> { inherit system; };

    in

    rec {
    aspellWithDicts = import ./pkgs/aspell-with-dicts;

    foo = import ./pkgs/foo {
    aspellWithDicts = aspellWithDicts;
    hello = pkgs.hello;
    stdenv = pkgs.stdenv;
    };
    }

    运行 shell 按预期工作: nix-shell ./custom-packages.nix -A foo --pure
    所以我的解决方案有效,但是这个结果可以以更简洁的惯用方式实现吗?

    最佳答案

    是否需要建foo ?什么在 foo你会用吗?

    假设您只想通过 nix-shell 使用 shell并且不想使用 nix-build 构建/安装任何东西或 nix-env -i ,这应该有效。

    以下shell.nix

    with import <nixpkgs> {};
    with pkgs;

    let
    myAspell = aspellWithDicts (d: [d.en]);
    in
    stdenv.mkDerivation {
    name = "myShell";

    buildInputs = [myAspell hello];

    shellHooks = ''
    echo Im in $name.
    echo aspell is locate at ${myAspell}
    echo hello is locate at ${hello}
    '';
    }

    会给你一个带有 aspell 的 shell 和 hello
    $ nix-shell
    Im in myShell.
    aspell is locate at /nix/store/zcclppbibcg4nfkis6zqml8cnrlnx00b-aspell-env
    hello is locate at /nix/store/gas2p68jqbzgb7zr96y5nc8j7nk61kkk-hello-2.10

    如果是这样的话 foo有一些代码来构建和安装。
    mkDerivationfoo/default.nix必须有 src字段可能是 src = ./.;或类似 fetchurlfetchFromGithub (有关示例,请参阅 document)。

    那么你可以使用 callPackagesimport (取决于 nix 表达式的编写方式)与 foo/default.nix作为论据带来什么 foo提供在此 shell 中使用。

    如果您尝试构建此 shell.nix (或 foo/default.nix )它会因丢失 src 而失败
    $ nix-build shell.nix 
    these derivations will be built:
    /nix/store/20h8cva19irq8vn39i72j8iz40ivijhr-myShell.drv
    building path(s) ‘/nix/store/r1f6qpxz91h5jkj7hzrmaymmzi9h1yml-myShell’
    unpacking sources
    variable $src or $srcs should point to the source
    builder for ‘/nix/store/20h8cva19irq8vn39i72j8iz40ivijhr-myShell.drv’ failed with exit code 1
    error: build of ‘/nix/store/20h8cva19irq8vn39i72j8iz40ivijhr-myShell.drv’ failed

    关于nix - 编写本地 nix 包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47591632/

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