Search code examples
nixnixoshome-manager

how to specify pkgs.writeShellApplication buildInputs at this condition?


I'm writing a nix module used by home-manager like this: { config, lib, pkgs, ... }:

let

  cfg = config.services.myservice;

  envFile = pkgs.writeTextFile {
    name = "myservice.env";
    text = ''
      ${cfg.environment}
    '';
  };

  configureScript = pkgs.writeShellApplication {
    name = "myservice-configure";
    runtimeInputs = [ pkgs.myprogram ] ++ (with pkgs; [
      bash
      coreutils
    ]);
    text = ''
      . ${envFile}
      myprogram --init-staff
    '';
  };

  startScript = pkgs.writeShellScriptBin "myservice-start" ''
    echo "Hello world" > /tmp/hello.log
  '';
in
with lib;
{
  options.services. = {
    enable = mkEnableOption "MyService Services";

    environment = mkOption {
      default = "";
      type = types.lines;
      description = ''
      EXEC_HOME=/tmp/myservice
      '';
    };
  };

  config = mkIf cfg.enable {
    home.packages = with pkgs;
      [
        myprogram
      ];
    systemd.user.services.myservice = {
      Install = {
        WantedBy = [ "default.target" ];
      };
      Service = {
        ExecStartPre = "${configureScript}/bin/myservice-configure";
        ExecStart = "${startScript}/bin/myservice-start";
      };
    };
  };
}

when I build the config with home-manager switch, it complains:

In /nix/store/9lkqwcsfhbq1hvm3jyk2jsn89nhkvx2w-myservice-configure/bin/myservice-configure line 9:
. /nix/store/cfn05s8cvhqaj7gnbnh44ncxwc6mwfwz-myservice.env
  ^-- SC1091 (info): Not following: /nix/store/cfn05s8cvhqaj7gnbnh44ncxwc6mwfwz-myservice.env was not specified as input (see shellcheck -x).

For more information:
  https://www.shellcheck.net/wiki/SC1091 -- Not following: /nix/store/cfn05s8...

I have tried to pass buildInputs like this:

  configureScript = pkgs.writeShellApplication {
    name = "myservice-configure";
    runtimeInputs = [ pkgs.myprogram ] ++ (with pkgs; [
      bash
      coreutils
    ]);
    derivationArgs = {
      buildInputs = [ envFile ];
    };
    text = ''
      . ${envFile}
      myprogram --init-staff
    '';
  };

but it doesn't work.

How to pass envFile as input of configureScript at this condition?

Many thanks.


Solution

  • Finally, I found a solution.

    First, there is no need to put envFile in buildInputs.

    Second, SC1091 can be cancelled by add a comment line just before the location where it is complained:

    text = ''
      # shellcheck disable=SC1091
      . ${envFile}
      myprogram --init-staff
    '';