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