liminix

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

default.nix (5202B)


      1 { config, pkgs, ... }:
      2 let
      3   inherit (pkgs)
      4     execline
      5     s6
      6     s6-init-bin
      7     s6-linux-init
      8     stdenvNoCC;
      9   inherit (pkgs.pseudofile) dir symlink;
     10   inherit (pkgs.liminix.services) bundle;
     11 
     12   s6-rc-db =
     13     let
     14       defaultDefaultTarget = bundle {
     15         name = "default";
     16         contents = builtins.attrValues config.services;
     17       };
     18       servicesAttrs = {
     19         default = defaultDefaultTarget;
     20       } // config.services;
     21     in
     22       pkgs.s6-rc-database.override {
     23         services = builtins.attrValues servicesAttrs;
     24       };
     25   s6-init-scripts = stdenvNoCC.mkDerivation {
     26     name = "s6-scripts";
     27     src = ./scripts;
     28     phases = ["unpackPhase" "installPhase" ];
     29     buildInputs = [];
     30     installPhase = ''
     31       mkdir $out
     32       cp -r $src $out/scripts
     33       chmod -R +w $out
     34     '';
     35   };
     36   service = dir  {
     37     s6-linux-init-runleveld = dir {
     38       notification-fd = { file = "3"; };
     39       run = {
     40         file = ''
     41               #!${execline}/bin/execlineb -P
     42               ${execline}/bin/fdmove -c 2 1
     43               ${execline}/bin/fdmove 1 3
     44               ${s6}/bin/s6-ipcserver -1 -a 0700 -c 1 -- s
     45               ${s6}/bin/s6-sudod -dt30000 -- "/etc/s6-linux-init/current"/scripts/runlevel
     46             '';
     47         mode = "0755";
     48       };
     49     };
     50     s6-linux-init-shutdownd = dir {
     51       fifo = {
     52         type = "i";
     53         mode = "0600";
     54       };
     55       run = {
     56         file = ''
     57               #!${execline}/bin/execlineb -P
     58               importas PATH PATH
     59               export PATH ${s6}/bin:''${PATH}
     60               foreground { echo path is  ''${PATH} }
     61               ${s6-linux-init}/bin/s6-linux-init-shutdownd -c  "/etc/s6-linux-init/current" -g 3000
     62             '';
     63         mode = "0755";
     64       };
     65     };
     66     s6-svscan-log = dir {
     67       fifo = {
     68         type = "i";
     69         mode = "0600";
     70       };
     71       notification-fd = { file = "3"; };
     72       run = {
     73         file = ''
     74               #!${execline}/bin/execlineb -P
     75               ${execline}/bin/redirfd -w 1 /dev/null
     76               ${execline}/bin/redirfd -rnb 0 fifo
     77               ${s6}/bin/s6-log -bpd3 -- t /run/uncaught-logs
     78           '';
     79         mode = "0755";
     80       };
     81     };
     82     getty = dir {
     83       run = {
     84         # We can't run a useful shell on /dev/console because
     85         # /dev/console is not allowed to be the controlling
     86         # tty of any process, which means ^C ^Z etc don't work.
     87         # So we work out what the *actual* console device is
     88         # using sysfs and open our shell there instead.
     89         file = ''
     90           #!${execline}/bin/execlineb -P
     91           ${execline}/bin/cd /
     92           redirfd -r 0 /sys/devices/virtual/tty/console/active
     93           withstdinas CONSOLETTY
     94           importas CONSOLETTY CONSOLETTY
     95           redirfd -w 2 /dev/''${CONSOLETTY}
     96           fdmove -c 1 2
     97           redirfd -r 0 /dev/''${CONSOLETTY}
     98           /bin/ash -l
     99         '';
    100         mode = "0755";
    101       };
    102       down-signal = {
    103         file = "HUP\n";
    104       };
    105     };
    106     ".s6-svscan" =
    107       let
    108         openConsole = ''
    109           #!${execline}/bin/execlineb -P
    110           ${execline}/bin/redirfd -w 2 /dev/console
    111           ${execline}/bin/fdmove -c 1 2
    112         '';
    113         quit = message: ''
    114           ${openConsole}
    115           ${execline}/bin/foreground { ${s6-linux-init}/bin/s6-linux-init-echo -- ${message} }
    116           ${s6-linux-init}/bin/s6-linux-init-hpr -fr
    117         '';
    118         shutdown = action: ''
    119           #!${execline}/bin/execlineb -P
    120           ${s6-linux-init}/bin/s6-linux-init-shutdown -a #{action} -- now
    121         '';
    122         empty = "#!${execline}/bin/execlineb -P\n";
    123       in dir {
    124         crash = {
    125           file = quit "s6-svscan crashed. Rebooting.";
    126           mode = "0755";
    127         };
    128         finish = {
    129           file = ''
    130             ${openConsole}
    131             ifelse { test -x /run/maintenance/exec } { /run/maintenance/exec }
    132             foreground { echo "s6-svscan exited. Rebooting." }
    133             wait { }
    134             ${s6-linux-init}/bin/s6-linux-init-hpr -fr
    135           '';
    136           mode = "0755";
    137         };
    138         SIGINT = {
    139           file = shutdown "-r";
    140           mode = "0755";
    141         };
    142         SIGPWR = {
    143           file = shutdown "-p";
    144           mode = "0755";
    145         };
    146         SIGQUIT = {
    147           file = empty;
    148           mode = "0755";
    149         };
    150         SIGTERM = {
    151           file = empty;
    152           mode = "0755";
    153         };
    154         SIGUSR1 = {
    155           file = shutdown "-p";
    156           mode = "0755";
    157         };
    158         SIGUSR2 = {
    159           file = shutdown "-h";
    160           mode = "0755";
    161         };
    162         SIGWINCH = {
    163           file = empty;
    164           mode = "0755";
    165         };
    166 
    167       };
    168   };
    169 in {
    170   config = {
    171     filesystem = dir {
    172       etc = dir {
    173         s6-rc = dir {
    174           compiled = symlink "${s6-rc-db}/compiled";
    175         };
    176         s6-linux-init = dir {
    177           current = dir {
    178             scripts = symlink "${s6-init-scripts}/scripts";
    179             env = dir {};
    180             run-image = dir {
    181               uncaught-logs = (dir {}) // {mode = "2750";};
    182               inherit service;
    183             };
    184           };
    185         };
    186       };
    187       bin = dir {
    188         init = symlink "${s6-init-bin}/bin/init";
    189       };
    190     };
    191   };
    192 }