nixos modules for convenient deployment of cloud resources

feat: add a function for generating apps from nixos configurations

ptr.pet 65414a3f 698c6ba2

verified
Changed files
+71 -1
firewall
provider
hetzner
+14
README.md
···
+
these are set of nixos modules for more convenient deployment of cloud resources for various providers. only implemented module is for firewalls right now, supporting hetzner.
+
+
## usage
+
+
if using flakes, put `nixosModules.<resource>` and `nixosModules.<resource>-<provider>`
+
in your NixOS configuration. for example, `nixosModules.firewall` and
+
`nixosModules.firewall-hetzner`. see `nix flake show` for all available modules.
+
+
if not using flakes, you can import `<resource>/` and `<resource>/provider/<provider>`.
+
+
then, you can either use each module's individual `mkApp` config option to
+
generate an app and run it, or you can call `makeApps`:
+
- for flakes use the flake output `makeApps` and `makeApps {inherit pkgs self;}`. you can assign the output of this to your `outputs.apps` as it generates flake apps.
+
- for non-flake use `import ./makeApps.nix {inherit pkgs nixosSystem;}`, this will return an attribute set with a `run` key which is the generated app.
+2 -1
firewall/provider/hetzner/default.nix
···
in {
options = {
providers.hetzner.firewall = {
+
enable = l.mkEnableOption "hetzner firewall";
id = l.mkOption {
type = t.ints.unsigned;
description = "The ID of the firewall to update.";
···
};
};
-
config = {
+
config = l.mkIf cfg.enable {
providers.hetzner.firewall.mkApp = pkgs: import ./app.nix {
inherit pkgs lib taggedPorts;
inherit (cfg) id;
+1
flake.nix
···
};
})
pkgsInstances;
+
makeApps = import ./makeApps.nix;
};
}
+54
makeApps.nix
···
+
{pkgs, lib ? pkgs.lib, self ? null, nixosSystem ? null}: let
+
l = lib;
+
mkProviderApp = provider:
+
l.concatStringsSep "\n" (l.flatten (
+
l.mapAttrsToList
+
(
+
name: module:
+
if module.enable
+
then ''
+
log info "deploying ${name} resource(s)..."
+
nu ${module.mkApp pkgs}
+
''
+
else []
+
)
+
provider
+
));
+
mkApp = {config, ...}: pkgs.writers.writeNu "deploy-resources" ''
+
use std/log
+
${
+
l.concatStringsSep "\n\n"
+
(
+
l.mapAttrsToList
+
(
+
name: provider: ''
+
log info "deploying resources for ${name}..."
+
${mkProviderApp provider}
+
''
+
)
+
config.providers
+
)
+
}
+
'';
+
in
+
if self != null
+
then
+
l.mergeAttrsList (
+
l.mapAttrsToList
+
(
+
hostname: host: {
+
"deploy-${hostname}-resources" = {
+
type = "app";
+
program = toString (mkApp host);
+
};
+
}
+
)
+
self.nixosConfigurations
+
)
+
else if nixosSystem != null
+
then
+
{
+
run = mkApp nixosSystem;
+
}
+
else
+
throw "nixos-cloud-resources: neither 'self' or 'nixosSystem' was provided, aborting"