make-service.nix (1061B)
1 { pkgs ? import <nixpkgs> { } }: 2 3 4 { name, services, supervise }: 5 with pkgs; let 6 createSupervise = writeScript "${name}-create-supervise" '' 7 #!/bin/sh 8 9 # create tmp/supervise.<service> (or whatever is specified) 10 mkdir -p ${lib.strings.concatStringsSep " " (map (sv: supervise sv.name) services)} 11 ''; 12 13 linkDir = sv: '' 14 # create out/<service> (later ./) 15 mkdir -p $out/${sv.name} 16 17 # change dir to ./out/<service> 18 cd $out/${sv.name} 19 20 # ./supervise ./run are mandatory, thus just unconditional linking 21 ln -s ${supervise sv.name} ./supervise 22 ln -s ${sv.run} ./run 23 24 # if ./setup is specified (not empty), link it 25 [ -n '${sv.setup}' ] && ln -s ${sv.setup} $out/${sv.name}/setup || true 26 27 # if not enabled, create empty file ./down 28 [ '${toString sv.enable}' = 'false' ] && touch $out/${sv.name}/down || true 29 ''; 30 linkDirs = lib.strings.concatStrings (map linkDir services); 31 32 in 33 runCommand name { } '' 34 #!/bin/sh 35 36 mkdir -p $out 37 ln -s ${createSupervise} $out/create-supervise.sh 38 39 ${linkDirs} 40 ''