From 08c6cb6e81a8d449411fbf56a77d512520d3c8e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20B=C3=A4dorf?= Date: Tue, 3 Oct 2023 13:50:01 +0200 Subject: [PATCH] feat: add deploy-rs config --- flake.nix | 22 ++++++++++++------ lib/default.nix | 26 +++++++++++++-------- lib/deploy.nix | 62 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 17 deletions(-) create mode 100644 lib/deploy.nix diff --git a/flake.nix b/flake.nix index 8a20d91f..55223341 100644 --- a/flake.nix +++ b/flake.nix @@ -69,6 +69,7 @@ devShells.default = pkgs.mkShell { buildInputs = [ + pkgs.deploy-rs pkgs.nixpkgs-fmt pkgs.agenix pkgs.ssh-to-age @@ -141,19 +142,26 @@ ]; }; - deploy.nodes = { - droppie.profiles.system = { - hostname = "backup.b12f.io"; - sshUser = "yule"; - path = inputs.deploy-rs.lib.x86_64-linux.activate.nixos self.nixosConfigurations.droppie; + deploy.nodes = self.b12f.lib.deploy.mkDeployNodes self.nixosConfigurations { + chocolatebar = { + sshUser = "b12f"; }; - pie.profiles.system = { + biolimo = { + sshUser = "b12f"; + }; + + droppie = { + hostname = "backup.b12f.io"; + sshUser = "yule"; + }; + + pie = { hostname = "pie.local"; sshUser = "yule"; }; - maoam.profiles.system = { + maoam = { sshUser = "b12f"; }; }; diff --git a/lib/default.nix b/lib/default.nix index ac167511..9edb1978 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -1,10 +1,16 @@ -{lib}: -lib.makeExtensible (self: let - callLibs = file: import file {lib = self;}; -in rec { - ## Define your own library functions here! - #id = x: x; - ## Or in files, containing functions that take {lib} - #foo = callLibs ./foo.nix; - ## In configs, they can be used under "lib.our" -}) +{ lib, inputs, ... }: { + # Configuration common to all Linux systems + flake = { + b12f.lib = let + callLibs = file: import file {inherit lib;}; + in rec { + ## Define your own library functions here! + #id = x: x; + ## Or in files, containing functions that take {lib} + #foo = callLibs ./foo.nix; + ## In configs, they can be used under "lib.our" + + deploy = import ./deploy.nix { inherit inputs lib; }; + }; + }; +} diff --git a/lib/deploy.nix b/lib/deploy.nix new file mode 100644 index 00000000..5e9f6418 --- /dev/null +++ b/lib/deploy.nix @@ -0,0 +1,62 @@ +/* + * The contents of this file are adapted from digga + * https://github.com/divnix/digga + * + * Licensed under the MIT license + */ + +{ lib, inputs }: let + getFqdn = c: let + net = c.config.networking; + fqdn = + if (net ? domain) && (net.domain != null) + then "${net.hostName}.${net.domain}" + else net.hostName; + in + fqdn; +in { + mkDeployNodes = systemConfigurations: extraConfig: + /* + * + Synopsis: mkNodes _systemConfigurations_ _extraConfig_ + + Generate the `nodes` attribute expected by deploy-rs + where _systemConfigurations_ are `nodes`. + + _systemConfigurations_ should take the form of a flake's + _nixosConfigurations_. Note that deploy-rs does not currently support + deploying to darwin hosts. + + _extraConfig_, if specified, will be merged into each of the + nodes' configurations. + + Example _systemConfigurations_ input: + + ``` + { + hostname-1 = { + fastConnection = true; + sshOpts = [ "-p" "25" ]; + }; + hostname-2 = { + sshOpts = [ "-p" "19999" ]; + sshUser = "root"; + }; + } + ``` + * + */ + lib.recursiveUpdate + (lib.mapAttrs + ( + _: c: { + hostname = getFqdn c; + profiles.system = { + user = "root"; + path = inputs.deploy-rs.lib.${c.pkgs.stdenv.hostPlatform.system}.activate.nixos c; + }; + } + ) + systemConfigurations) + extraConfig; +}