From 79524242dc2fc1ca9290d4bdecf438f094764558 Mon Sep 17 00:00:00 2001 From: Ingolf Wagner Date: Fri, 6 Mar 2020 14:10:55 +0800 Subject: [PATCH] krops: udpate shell to make it more powerful --- shell.nix | 102 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 61 insertions(+), 41 deletions(-) diff --git a/shell.nix b/shell.nix index 2e77bbc..e64b1bb 100644 --- a/shell.nix +++ b/shell.nix @@ -124,33 +124,48 @@ let hostPattern = name: if name == "porani" then "${name}.insecure" else "${name}.private"; - deployment = populateCommands: name: + deployment = { secrets, content }: + name: { host ? (hostPattern name), target ? "/var/src/", user ? "root" - , commandPrefix ? "deploy", enableSwitch ? true }: + , commandPrefix ? "deploy", enableSwitch ? true, enableSecrets ? true }: with ops; - jobs "${commandPrefix}-${name}" "${user}@${host}${target}" - (populateCommands ++ (if enableSwitch then [ switch ] else [ ])); + let + commandName = if enableSecrets then + "${commandPrefix}-${name}-with-secrets" + else + "${commandPrefix}-${name}-without-secrets"; + populateCommands = with lib; flatten [ + content + (optionals enableSecrets secrets) + (optionals enableSwitch [ switch ]) + ]; + in jobs commandName "${user}@${host}${target}" populateCommands; serverDeployment = name: with ops; - deployment [ - (populate source.nixPkgs) - (populate source.modules) - (populate (source.secrets name)) - (populate (source.system name)) - ] name; + deployment { + content = [ + (populate source.nixPkgs) + (populate source.modules) + (populate (source.system name)) + ]; + secrets = [ (populate (source.secrets name)) ]; + } name; desktopDeployment = name: with ops; - deployment [ - (populate source.nixPkgs) - (populate source.modules) - (populate (source.secrets name)) - (populate (source.system name)) - (populate source.desktopSecrets) - ] name; + deployment { + content = [ + (populate source.nixPkgs) + (populate source.modules) + (populate (source.system name)) + ]; + secrets = + [ (populate (source.secrets name)) (populate source.desktopSecrets) ]; + } name; cleanupNix = name: + { ... }: let target = { host = hostPattern name; @@ -165,40 +180,45 @@ let nix-collect-garbage -d ''; - # generate tasks + # helper function to make stuff more readable + runForAll = serverList: command: arguments: + let f = args: (map (name: command name (arguments // args)) serverList); + in (f { enableSecrets = false; }) ++ (f { enableSecrets = true; }); - servers = with lib; + # generate tasks + servers = serverList: + with lib; let - serverList = [ "workhorse" "sputnik" "porani" ]; - deployments = flip map serverList (name: serverDeployment name { }); - cleanup = flip map serverList (name: cleanupNix name); - install = flip map serverList (name: - serverDeployment name { - commandPrefix = "install"; - host = installHost; - target = "/mnt/var/src"; - enableSwitch = false; - }); + doForAll = runForAll serverList; + deployments = doForAll serverDeployment { }; + cleanup = doForAll cleanupNix { }; + install = doForAll serverDeployment { + commandPrefix = "install"; + host = installHost; + target = "/mnt/var/src"; + enableSwitch = false; + }; in deployments ++ cleanup ++ install; - desktops = with lib; + desktops = desktopList: + with lib; let - desktopList = [ "pepe" "workout" "sterni" "mobi" ]; - deployments = flip map desktopList (name: desktopDeployment name { }); - cleanup = flip map desktopList (name: cleanupNix name); - install = flip map desktopList (name: - desktopDeployment name { - commandPrefix = "install"; - host = installHost; - target = "/mnt/var/src"; - enableSwitch = false; - }); + doForAll = runForAll desktopList; + deployments = doForAll desktopDeployment { }; + cleanup = doForAll cleanupNix { }; + install = doForAll desktopDeployment { + commandPrefix = "install"; + host = installHost; + target = "/mnt/var/src"; + enableSwitch = false; + }; in deployments ++ cleanup ++ install; in pkgs.mkShell { buildInputs = with pkgs; - servers ++ desktops ++ [ + (servers [ "workhorse" "sputnik" "porani" ]) + ++ (desktops [ "pepe" "workout" "sterni" "mobi" ]) ++ [ (pkgs.writers.writeBashBin "reformat" '' find ${ toString ./.