7 makeScript = name: service: pkgs.writeScript "${name}-runner"
9 #! ${pkgs.perl.withPackages (p: [ p.FileSlurp ])}/bin/perl -w
16 while ($cmd =~ /([^ \t\n']+)|(\'([^'])\')\s*/g) {
20 if (substr($args[0], 0, 1) eq "@") {
21 $prog = substr($args[0], 1);
28 setpgrp; # don't receive SIGINT etc. from terminal
30 die "failed to exec $prog\n";
31 } elsif (!defined $pid) {
32 die "failed to fork: $!\n";
40 die if waitpid($pid, 0) != $pid;
44 # Set the environment. FIXME: escaping.
45 foreach my $key (keys %ENV) {
46 next if $key eq 'LOCALE_ARCHIVE';
49 ${concatStrings (mapAttrsToList (n: v: ''
50 $ENV{'${n}'} = '${v}';
51 '') service.environment)}
53 # Run the ExecStartPre program. FIXME: this could be a list.
54 my $preStart = <<END_CMD;
55 ${concatStringsSep "\n" (service.serviceConfig.ExecStartPre or [])}
57 if (defined $preStart && $preStart ne "\n") {
58 print STDERR "running ExecStartPre: $preStart\n";
59 my $res = run_wait $preStart;
60 die "$0: ExecStartPre failed with status $res\n" if $res;
63 # Run the ExecStart program.
65 ${service.serviceConfig.ExecStart}
68 print STDERR "running ExecStart: $cmd\n";
69 my $mainPid = run $cmd;
70 $ENV{'MAINPID'} = $mainPid;
72 # Catch SIGINT, propagate to the main program.
74 print STDERR "got SIGINT, stopping service...\n";
77 $SIG{'INT'} = \&intHandler;
78 $SIG{'QUIT'} = \&intHandler;
80 # Run the ExecStartPost program.
81 my $postStart = <<END_CMD;
82 ${concatStringsSep "\n" (service.serviceConfig.ExecStartPost or [])}
84 if (defined $postStart && $postStart ne "\n") {
85 print STDERR "running ExecStartPost: $postStart\n";
86 my $res = run_wait $postStart;
87 die "$0: ExecStartPost failed with status $res\n" if $res;
90 # Wait for the main program to exit.
91 die if waitpid($mainPid, 0) != $mainPid;
94 # Run the ExecStopPost program.
95 my $postStop = <<END_CMD;
96 ${service.serviceConfig.ExecStopPost or ""}
98 if (defined $postStop && $postStop ne "\n") {
99 print STDERR "running ExecStopPost: $postStop\n";
100 my $res = run_wait $postStop;
101 die "$0: ExecStopPost failed with status $res\n" if $res;
104 exit($mainRes & 127 ? 255 : $mainRes << 8);
107 opts = { config, name, ... }: {
108 options.runner = mkOption {
111 A script that runs the service outside of systemd,
112 useful for testing or for using NixOS services outside
116 config.runner = makeScript name config;
123 systemd.services = mkOption {
124 type = with types; attrsOf (submodule opts);