【问题标题】:How do I create a file in the nix store?如何在 nix 商店中创建文件?
【发布时间】:2022-08-17 01:06:23
【问题描述】:

我想在一个 nix 文件中定义/管理一个配置文件,并让它在 nix 商店中结束,我该怎么做?

目前,我正在通过将文件放在~/example/config.json 之类的路径中手动管理这个(在 nix 商店之外),并使用如下:

  systemd.services = {
   example = {
      description = \"abcxyz\";
      serviceConfig = {
        WorkingDirectory = \"%h/example/\";
        Type      = \"simple\";
        ExecStart = \"${example-pkg}/bin/app -c config.json\";
        Restart   = \"always\";
        RestartSec   = 60;
      };
      wantedBy = [ \"default.target\" ];
    };

例如,Nixos 定义了一个 postgresql 服务,该服务的配置文件位于 /nix/store/pcp1r7f8mylwvri381an01r64knj1wwb-postgresql.conf/postgresql.conf - 我如何在上面自己的服务中复制此功能?

看看https://github.com/NixOS/nixpkgs/blob/226ff16bd6fbe6d47e714379eeba8598cd61a90a/nixos/modules/services/databases/postgresql.nix#L21

configFile = pkgs.writeTextDir \"postgresql.conf\" (concatStringsSep \"\\n\" (mapAttrsToList (n: v: \"${n} = ${toStr v}\") cfg.settings));

我不清楚configFile 是什么?它会是postgresql.conf 文件的路径吗?

无论哪种方式pkgs.writeTextDir \"config.ini\" 似乎都创建了一个目录,我应该改用pkgs.writeText 吗?

    标签: nix nixos


    【解决方案1】:

    以上似乎工作:

      configFile = pkgs.writeText "config.ini" 
        ''
        example config file bla bla
        '';
    

    然后你可以通过字符串插值使用configFile

    ExecStart = "${example-pkg}/bin/app -c ${configFile}";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-26
      • 1970-01-01
      • 2012-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多