liminix

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

common.nix (2912B)


      1 {
      2   writeAshScript,
      3   liminix,
      4   svc,
      5   lib,
      6   serviceFns,
      7   output-template,
      8 }:
      9 {
     10   command,
     11   name,
     12   debug,
     13   username,
     14   password,
     15   lcpEcho,
     16   bandwidth,
     17   ppp-options,
     18   dependencies ? [ ],
     19 }:
     20 let
     21   inherit (lib)
     22     optional
     23     optionals
     24     escapeShellArgs
     25     ;
     26   inherit (liminix.services) longrun;
     27   inherit (builtins) toJSON toString typeOf;
     28 
     29   ip-up = writeAshScript "ip-up" { } ''
     30     exec >&5 2>&5
     31     . ${serviceFns} 
     32     in_outputs ${name}
     33     echo $1 > ifname
     34     echo $2 > tty
     35     echo $3 > speed
     36     echo $4 > address
     37     echo $5 > peer-address
     38     cat /sys/class/net/$1/ifindex > ifindex
     39     set +o nounset
     40     if test -n "''${DNS1}" ;then echo ''${DNS1} > ns1 ; fi
     41     if test -n "''${DNS2}" ;then echo ''${DNS2} > ns2 ; fi
     42     touch ip-up
     43     test -e ipv6-up && ( echo >/proc/self/fd/10 || true)
     44   '';
     45   ip6-up = writeAshScript "ip6-up" { } ''
     46     exec >&5 2>&5
     47     . ${serviceFns} 
     48     in_outputs ${name}
     49     echo $5 > ipv6-peer-address
     50     echo $4 > ipv6-address
     51     touch ipv6-up
     52     test -e ip-up && ( echo >/proc/self/fd/10 || true)
     53   '';
     54   literal_or_output =
     55     let
     56       v =
     57         o:
     58         (
     59           {
     60             string = toJSON;
     61             int = toJSON;
     62             lambda = (o: "output(${toJSON (o "service")}, ${toJSON (o "path")})");
     63           }
     64           .${typeOf o}
     65         )
     66           o;
     67     in
     68     o: "{{ ${v o} }}";
     69 
     70   ppp-options' = [
     71     "+ipv6"
     72     "noauth"
     73   ]
     74   ++ optional debug "debug"
     75   ++ optionals (username != null) [
     76     "name"
     77     (literal_or_output username)
     78   ]
     79   ++ optionals (password != null) [
     80     "password"
     81     (literal_or_output password)
     82   ]
     83   ++ optional lcpEcho.adaptive "lcp-echo-adaptive"
     84   ++ optionals (lcpEcho.interval != null) [
     85     "lcp-echo-interval"
     86     (toString lcpEcho.interval)
     87   ]
     88   ++ optionals (lcpEcho.failure != null) [
     89     "lcp-echo-failure"
     90     (toString lcpEcho.failure)
     91   ]
     92   ++ ppp-options
     93   ++ [
     94     "ip-up-script"
     95     ip-up
     96     "ipv6-up-script"
     97     ip6-up
     98     "ipparam"
     99     name
    100     "nodetach"
    101     # usepeerdns requests DNS servers from peer (which is good),
    102     # then attempts to write them to /nix/store/xxxx/ppp/resolv.conf
    103     # which causes an unsightly but inconsequential error message
    104     "usepeerdns"
    105     "nodefaultroute"
    106     "logfd"
    107     "2"
    108   ];
    109   service = longrun {
    110     inherit name;
    111     run = ''
    112       mkdir -p /run/${name}
    113       chmod 0700 /run/${name}
    114       in_outputs ${name}
    115       echo ${escapeShellArgs ppp-options'} | ${output-template}/bin/output-template '{{' '}}' > /run/${name}/ppp-options
    116       fdmove -c 5 2 \
    117       ${command}
    118     '';
    119     notification-fd = 10;
    120     properties.bandwidth = bandwidth;
    121     timeout-up =
    122       if lcpEcho.failure != null then (10 + lcpEcho.failure * lcpEcho.interval) * 1000 else 60 * 1000;
    123     inherit dependencies;
    124   };
    125 in
    126 svc.secrets.subscriber.build {
    127   watch = lib.filter (n: typeOf n == "lambda") [
    128     username
    129     password
    130   ];
    131   inherit service;
    132 }