Commit af04a80c authored by Robert Hensing's avatar Robert Hensing
Browse files

modular-services: merge process.argv from executable and args

parent b9e4118e
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -48,7 +48,7 @@ It is possible to write service modules that are portable. This is done by eithe
{ config, options, lib, ... }: {
  _class = "service";
  config = {
    process.executable = "${lib.getExe config.foo.program}";
    process.argv = [ (lib.getExe config.foo.program) ];
  } // lib.optionalAttrs (options?systemd) {
    # ... systemd-specific definitions ...
  };
+6 −21
Original line number Diff line number Diff line
@@ -7,18 +7,6 @@
let
  inherit (lib) mkOption types;
  pathOrStr = types.coercedTo types.path (x: "${x}") types.str;
  program =
    types.coercedTo (
      types.package
      // {
        # require mainProgram for this conversion
        check = v: v.type or null == "derivation" && v ? meta.mainProgram;
      }
    ) lib.getExe pathOrStr
    // {
      description = "main program, path or command";
      descriptionClass = "conjunction";
    };
in
{
  # https://nixos.org/manual/nixos/unstable/#modular-services
@@ -45,18 +33,15 @@ in
      visible = "shallow";
    };
    process = {
      executable = mkOption {
        type = program;
        description = ''
          The path to the executable that will be run when the service is started.
        '';
      };
      args = lib.mkOption {
      argv = lib.mkOption {
        type = types.listOf pathOrStr;
        example = lib.literalExpression ''[ (lib.getExe config.package) "--nobackground" ]'';
        description = ''
          Arguments to pass to the `executable`.
          Command filename and arguments for starting this service.
          This is a raw command-line that should not contain any shell escaping.
          If expansion of environmental variables is required then use
          a shell script or `importas` from `pkgs.execline`.
        '';
        default = [ ];
      };
    };
  };
+31 −18
Original line number Diff line number Diff line
@@ -20,8 +20,10 @@ let
    services = {
      service1 = {
        process = {
          executable = "/usr/bin/echo"; # *giggles*
          args = [ "hello" ];
          argv = [
            "/usr/bin/echo" # *giggles*
            "hello"
          ];
        };
        assertions = [
          {
@@ -37,21 +39,27 @@ let
        process = {
          # No meta.mainProgram, because it's supposedly an executable script _file_,
          # not a directory with a bin directory containing the main program.
          executable = dummyPkg "cowsay.sh";
          args = [ "world" ];
          argv = [
            (dummyPkg "cowsay.sh")
            "world"
          ];
        };
      };
      service3 = {
        process = {
          executable = "/bin/false";
          args = [ ];
          argv = [ "/bin/false" ];
        };
        services.exclacow = {
          process = {
            executable = dummyPkg "cowsay-ng" // {
            argv = [
              (lib.getExe (
                dummyPkg "cowsay-ng"
                // {
                  meta.mainProgram = "cowsay";
            };
            args = [ "!" ];
                }
              ))
              "!"
            ];
          };
          assertions = [
            {
@@ -91,8 +99,10 @@ let
        services = {
          service1 = {
            process = {
              executable = "/usr/bin/echo";
              args = [ "hello" ];
              argv = [
                "/usr/bin/echo"
                "hello"
              ];
            };
            services = { };
            assertions = [
@@ -107,8 +117,10 @@ let
          };
          service2 = {
            process = {
              executable = "${dummyPkg "cowsay.sh"}";
              args = [ "world" ];
              argv = [
                "${dummyPkg "cowsay.sh"}"
                "world"
              ];
            };
            services = { };
            assertions = [ ];
@@ -116,13 +128,14 @@ let
          };
          service3 = {
            process = {
              executable = "/bin/false";
              args = [ ];
              argv = [ "/bin/false" ];
            };
            services.exclacow = {
              process = {
                executable = "${dummyPkg "cowsay-ng"}/bin/cowsay";
                args = [ "!" ];
                argv = [
                  "${dummyPkg "cowsay-ng"}/bin/cowsay"
                  "!"
                ];
              };
              services = { };
              assertions = [
+1 −3
Original line number Diff line number Diff line
@@ -69,9 +69,7 @@ in
        Restart = lib.mkDefault "always";
        RestartSec = lib.mkDefault "5";
        ExecStart = [
          (systemdPackage.functions.escapeSystemdExecArgs (
            [ config.process.executable ] ++ config.process.args
          ))
          (systemdPackage.functions.escapeSystemdExecArgs config.process.argv)
        ];
      };
    };
+9 −6
Original line number Diff line number Diff line
@@ -11,14 +11,17 @@
let
  machine = evalSystem (
    { lib, ... }:
    let
      hello' = lib.getExe hello;
    in
    {

      # Test input

      system.services.foo = {
        process = {
          executable = hello;
          args = [
          argv = [
            hello'
            "--greeting"
            "hoi"
          ];
@@ -26,8 +29,8 @@ let
      };
      system.services.bar = {
        process = {
          executable = hello;
          args = [
          argv = [
            hello'
            "--greeting"
            "hoi"
          ];
@@ -37,8 +40,8 @@ let
        };
        services.db = {
          process = {
            executable = hello;
            args = [
            argv = [
              hello'
              "--greeting"
              "Hi, I'm a database, would you believe it"
            ];
Loading