From 9c30b6f8827756b79bd41cdf49781493c712804a Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Fri, 23 Aug 2024 22:25:57 +0100 Subject: [PATCH] change output references from attrset to lambda this is so that we can distinguish a ref from a literal parameter that might be a attrset --- examples/router-with-l2tp.nix | 102 ++++++++++++++++------------------ modules/hostapd/service.nix | 2 +- modules/ppp/default.nix | 9 +-- modules/ppp/l2tp.nix | 2 +- modules/ppp/pppoe.nix | 2 +- pkgs/default.nix | 19 +++---- 6 files changed, 63 insertions(+), 73 deletions(-) diff --git a/examples/router-with-l2tp.nix b/examples/router-with-l2tp.nix index 36644c6..24aa72f 100644 --- a/examples/router-with-l2tp.nix +++ b/examples/router-with-l2tp.nix @@ -30,6 +30,7 @@ lns = { hostname = "l2tp.aaisp.net.uk"; address = "194.4.172.12"; }; inherit (pkgs.liminix.services) oneshot longrun target; + inherit (pkgs.liminix) outputRef; inherit (pkgs.pseudofile) dir symlink; inherit (pkgs) serviceFns; svc = config.system.service; @@ -99,53 +100,53 @@ in rec { localDomain = "lan"; }; }; - wan = { - interface = let - secret = path: { service = config.services.secrets; inherit path; }; - pppoe = svc.pppoe.build { - interface = config.hardware.networkInterfaces.wan; - debug = true; - username = secret "ppp/username"; - password = secret "ppp/password"; - }; - - l2tp = + wan = + let + secret = outputRef config.services.secrets; + username = secret "ppp/username"; + password = secret "ppp/password"; + in { + interface = let - check-address = oneshot rec { - name = "check-lns-address"; - up = "grep -Fx ${lns.address} $(output_path ${services.lns-address} addresses)"; - dependencies = [ services.lns-address ]; + pppoe = svc.pppoe.build { + interface = config.hardware.networkInterfaces.wan; + debug = true; + inherit username password; }; - route = svc.network.route.build { - via = "$(output ${services.bootstrap-dhcpc} router)"; - target = lns.address; - dependencies = [services.bootstrap-dhcpc check-address]; - }; - l2tpd= svc.l2tp.build { - lns = lns.address; - ppp-options = [ - "debug" "+ipv6" "noauth" - "name" rsecrets.l2tp.name - "password" rsecrets.l2tp.password - ]; - dependencies = [config.services.lns-address route check-address]; - }; - in - svc.health-check.build { - service = l2tpd; - threshold = 3; - interval = 2; - healthCheck = pkgs.writeAshScript "ping-check" {} "ping 1.1.1.1"; - }; - in svc.round-robin.build { - name = "wan"; - services = [ - pppoe - l2tp - ]; + + l2tp = + let + check-address = oneshot rec { + name = "check-lns-address"; + up = "grep -Fx ${lns.address} $(output_path ${services.lns-address} addresses)"; + dependencies = [ services.lns-address ]; + }; + route = svc.network.route.build { + via = "$(output ${services.bootstrap-dhcpc} router)"; + target = lns.address; + dependencies = [services.bootstrap-dhcpc check-address]; + }; + l2tpd= svc.l2tp.build { + lns = lns.address; + inherit username password; + dependencies = [config.services.lns-address route check-address]; + }; + in + svc.health-check.build { + service = l2tpd; + threshold = 3; + interval = 2; + healthCheck = pkgs.writeAshScript "ping-check" {} "ping 1.1.1.1"; + }; + in svc.round-robin.build { + name = "wan"; + services = [ + pppoe + l2tp + ]; + }; + dhcp6.enable = true; }; - dhcp6.enable = true; - }; wireless.networks = { "${rsecrets.ssid}" = { @@ -153,11 +154,8 @@ in rec { hw_mode = "g"; channel = "6"; ieee80211n = 1; - } // wirelessConfig //{ - wpa_passphrase = { - service = config.services.secrets; - path = "wpa_passphrase"; - }; + } // wirelessConfig // { + wpa_passphrase = outputRef config.services.secrets "wpa_passphrase"; }; "${rsecrets.ssid}5" = rec { @@ -170,15 +168,11 @@ in rec { ieee80211n = 1; ieee80211ac = 1; } // wirelessConfig // { - wpa_passphrase = { - service = config.services.secrets; - path = "wpa_passphrase"; - }; + wpa_passphrase = outputRef config.services.secrets "wpa_passphrase"; }; }; }; - services.bootstrap-dhcpc = svc.network.dhcp.client.build { interface = config.services.wwan; dependencies = [ config.services.hostname ]; diff --git a/modules/hostapd/service.nix b/modules/hostapd/service.nix index d4edb2e..4d0e261 100644 --- a/modules/hostapd/service.nix +++ b/modules/hostapd/service.nix @@ -30,7 +30,7 @@ let literal_or_output = o: ({ string = builtins.toJSON; int = builtins.toJSON; - set = (o: "output(${builtins.toJSON o.service}, ${builtins.toJSON o.path})"); + lambda = (o: "output(${builtins.toJSON (o "service")}, ${builtins.toJSON (o "path")})"); }.${builtins.typeOf o}) o; conf = diff --git a/modules/ppp/default.nix b/modules/ppp/default.nix index e609406..fdbe121 100644 --- a/modules/ppp/default.nix +++ b/modules/ppp/default.nix @@ -40,12 +40,12 @@ in { description = "ethernet interface to run PPPoE over"; }; username = mkOption { - type = types.nullOr liminix.lib.types.replacable; + type = types.nullOr (liminix.lib.types.replacable types.str); default = null; description = "username"; }; password = mkOption { - type = types.nullOr liminix.lib.types.replacable; + type = types.nullOr (liminix.lib.types.replacable types.str); default = null; description = "password"; }; @@ -83,12 +83,12 @@ in { description = "hostname or address of the L2TP network server"; }; username = mkOption { - type = types.nullOr liminix.lib.types.replacable; + type = types.nullOr (liminix.lib.types.replacable types.str); default = null; description = "username"; }; password = mkOption { - type = types.nullOr liminix.lib.types.replacable; + type = types.nullOr (liminix.lib.types.replacable types.str); default = null; description = "password"; }; @@ -116,6 +116,7 @@ in { }; ppp-options = mkOption { type = types.listOf types.str; + default = []; description = "options supplied on ppp command line"; }; }; diff --git a/modules/ppp/l2tp.nix b/modules/ppp/l2tp.nix index 78da1a8..6f50851 100644 --- a/modules/ppp/l2tp.nix +++ b/modules/ppp/l2tp.nix @@ -44,7 +44,7 @@ let let v = o: ({ string = builtins.toJSON; int = builtins.toJSON; - set = (o: "output(${builtins.toJSON o.service}, ${builtins.toJSON o.path})"); + lambda = (o: "output(${builtins.toJSON (o "service")}, ${builtins.toJSON (o "path")})"); }.${builtins.typeOf o}) o; in o: "{{ ${v o} }}"; diff --git a/modules/ppp/pppoe.nix b/modules/ppp/pppoe.nix index 772067f..a152918 100644 --- a/modules/ppp/pppoe.nix +++ b/modules/ppp/pppoe.nix @@ -44,7 +44,7 @@ let let v = o: ({ string = builtins.toJSON; int = builtins.toJSON; - set = (o: "output(${builtins.toJSON o.service}, ${builtins.toJSON o.path})"); + lambda = (o: "output(${builtins.toJSON (o "service")}, ${builtins.toJSON (o "path")})"); }.${builtins.typeOf o}) o; in o: "{{ ${v o} }}"; ppp-options' = diff --git a/pkgs/default.nix b/pkgs/default.nix index a43e785..4657283 100644 --- a/pkgs/default.nix +++ b/pkgs/default.nix @@ -14,6 +14,9 @@ in { uimage = callPackage ./kernel/uimage.nix { }; kernel = callPackage ./kernel { }; }; + outputRef = service : path : + let h = { inherit service path; }; + in x : h.${x}; callService = path : parameters : let pkg = callPackage path {}; checkTypes = t : p : typeChecked (builtins.toString path) t p; @@ -43,18 +46,10 @@ in { description = "parametrisable s6-rc service definition"; check = x: lib.isAttrs x && x ? parameters && x ? build; }; - replacable = types.either - types.str - (types.submodule { - options = { - service = mkOption { - type = service; - }; - path = mkOption { - type = types.str; - }; - }; - }); + replacable = t : types.either + t + # function might return a service or a path + (types.functionTo types.anything); }; inherit typeChecked; };