flake-parts/setups: set separate namespace for custom library

This at least allows us to make custom environment-specific library sets.
This commit is contained in:
Gabriel Arazas 2024-02-11 15:16:25 +08:00
parent 95b3e7bc2e
commit a4177f9382
No known key found for this signature in database
GPG Key ID: ADE0C41DAB221FCC
27 changed files with 151 additions and 123 deletions

View File

@ -6,7 +6,7 @@
let let
pkgs = import <nixpkgs> { }; pkgs = import <nixpkgs> { };
config' = import <config> { }; config' = import <config> { };
lib = pkgs.lib.extend (import <config/lib/extras/extend-lib.nix>); lib = pkgs.lib;
in in
import <nixpkgs/nixos/lib/eval-config.nix> { import <nixpkgs/nixos/lib/eval-config.nix> {
inherit lib; inherit lib;
@ -41,9 +41,14 @@ import <nixpkgs/nixos/lib/eval-config.nix> {
<sops-nix/modules/home-manager/sops.nix> <sops-nix/modules/home-manager/sops.nix>
({ config, lib, ... }: { ({ config, lib, ... }: {
xdg.userDirs.createDirectories = lib.mkForce true; xdg.userDirs.createDirectories = lib.mkForce true;
_module.args.foodogsquaredLib =
import ../../lib/extras/home-manager-set.nix { inherit lib; };
}) })
]; ];
_module.args.foodogsquaredLib =
import ../../lib/extras/nixos-set.nix { inherit lib; };
workflows.workflows.${workflow}.enable = true; workflows.workflows.${workflow}.enable = true;
nixpkgs.overlays = [ nixpkgs.overlays = [

View File

@ -1,4 +1,4 @@
{ config, lib, options, pkgs, ... }: { config, lib, pkgs, foodogsquaredLib, ... }:
{ {
imports = [ ./modules ]; imports = [ ./modules ];
@ -32,7 +32,7 @@
# The keyfile required to decrypt the secrets. # The keyfile required to decrypt the secrets.
sops.age.keyFile = "${config.xdg.configHome}/age/user"; sops.age.keyFile = "${config.xdg.configHome}/age/user";
sops.secrets = lib.private.getSecrets ./secrets/secrets.yaml { sops.secrets = foodogsquaredLib.sops-nix.getSecrets ./secrets/secrets.yaml {
davfs2-credentials = { davfs2-credentials = {
path = "${config.home.homeDirectory}/.davfs2/davfs2.conf"; path = "${config.home.homeDirectory}/.davfs2/davfs2.conf";
}; };

View File

@ -1,9 +1,9 @@
# This is the user that is often used for servers. # This is the user that is often used for servers.
{ lib, pkgs, ... }: { lib, pkgs, foodogsquaredLib, ... }:
let let
user = "plover"; user = "plover";
homeManagerUser = lib.private.getConfig "home-manager" user; homeManagerUser = foodogsquaredLib.getConfig "home-manager" user;
in in
{ {
users.users.${user} = { users.users.${user} = {

View File

@ -1,4 +1,4 @@
{ config, pkgs, lib, foodogsquaredModulesPath, ... }: { config, pkgs, lib, foodogsquaredLib, foodogsquaredModulesPath, ... }:
{ {
imports = [ imports = [
@ -33,7 +33,7 @@
type = "ed25519"; type = "ed25519";
}]; }];
sops.secrets = lib.private.getSecrets ./secrets/secrets.yaml { sops.secrets = foodogsquaredLib.sops-nix.getSecrets ./secrets/secrets.yaml {
"ssh-key" = { }; "ssh-key" = { };
}; };

View File

@ -1,4 +1,4 @@
{ config, lib, pkgs, ... }: { config, lib, pkgs, foodogsquaredLib, ... }:
let let
hostCfg = config.hosts.ni; hostCfg = config.hosts.ni;
@ -31,7 +31,7 @@ in
{ {
environment.systemPackages = with pkgs; [ wireguard-tools ]; environment.systemPackages = with pkgs; [ wireguard-tools ];
networking.firewall.allowedUDPPorts = [ wireguardPort ]; networking.firewall.allowedUDPPorts = [ wireguardPort ];
sops.secrets = lib.private.getSecrets ../../secrets/secrets.yaml { sops.secrets = foodogsquaredLib.sops-nix.getSecrets ../../secrets/secrets.yaml {
"wireguard/private-key" = { }; "wireguard/private-key" = { };
"wireguard/preshared-keys/plover" = { }; "wireguard/preshared-keys/plover" = { };
"wireguard/preshared-keys/phone" = { }; "wireguard/preshared-keys/phone" = { };

View File

@ -1,5 +1,5 @@
# It's a setup for my backup. # It's a setup for my backup.
{ config, lib, pkgs, ... }: { config, lib, pkgs, foodogsquaredLib, ... }:
let let
hostCfg = config.hosts.ni; hostCfg = config.hosts.ni;
@ -47,9 +47,9 @@ in
lib.mkEnableOption "backup setup with BorgBackup"; lib.mkEnableOption "backup setup with BorgBackup";
config = lib.mkIf cfg.enable { config = lib.mkIf cfg.enable {
sops.secrets = lib.private.getSecrets sops.secrets = foodogsquaredLib.sops-nix.getSecrets
./secrets.yaml ./secrets.yaml
(lib.private.attachSopsPathPrefix pathPrefix { (foodogsquaredLib.sops-nix.attachSopsPathPrefix pathPrefix {
"patterns/home" = { }; "patterns/home" = { };
"patterns/etc" = { }; "patterns/etc" = { };
"patterns/keys" = { }; "patterns/keys" = { };
@ -94,18 +94,18 @@ in
startAt = "04:30"; startAt = "04:30";
}; };
remote-backup-hetzner-box = borgJobCommonSetting #remote-backup-hetzner-box = borgJobCommonSetting
{ # {
patterns = with config.sops; [ # patterns = with config.sops; [
secrets."${pathPrefix}/patterns/remote-backup".path # secrets."${pathPrefix}/patterns/remote-backup".path
]; # ];
passCommand = "cat ${config.sops.secrets."${pathPrefix}/repos/hetzner-box/password".path}"; # passCommand = "cat ${config.sops.secrets."${pathPrefix}/repos/hetzner-box/password".path}";
} // { # } // {
doInit = true; # doInit = true;
repo = "ssh://${hetzner-boxes-user}@${hetzner-boxes-server}:23/./borg/desktop/ni"; # repo = "ssh://${hetzner-boxes-user}@${hetzner-boxes-server}:23/./borg/desktop/ni";
startAt = "04:30"; # startAt = "04:30";
environment.BORG_RSH = "ssh -i ${config.sops.secrets."${pathPrefix}/ssh-key".path}"; # environment.BORG_RSH = "ssh -i ${config.sops.secrets."${pathPrefix}/ssh-key".path}";
}; #};
}; };
programs.ssh.extraConfig = '' programs.ssh.extraConfig = ''

View File

@ -1,4 +1,4 @@
{ config, lib, pkgs, ... }: { config, lib, pkgs, foodogsquaredLib, ... }:
let let
hostCfg = config.hosts.ni; hostCfg = config.hosts.ni;
@ -95,7 +95,7 @@ in
{ {
environment.systemPackages = [ ytdlpArchiveVariant ]; environment.systemPackages = [ ytdlpArchiveVariant ];
sops.secrets = lib.private.getSecrets ./secrets.yaml sops.secrets = foodogsquaredLib.sops-nix.getSecrets ./secrets.yaml
(lib.attachSopsPathPrefix pathPrefix { (lib.attachSopsPathPrefix pathPrefix {
"secrets-config" = { }; "secrets-config" = { };
}); });

View File

@ -1,4 +1,4 @@
{ config, lib, pkgs, foodogsquaredModulesPath, ... }: { config, lib, pkgs, foodogsquaredLib, foodogsquaredModulesPath, ... }:
{ {
imports = [ imports = [
@ -8,8 +8,8 @@
./modules/profiles/hetzner-cloud-cx21.nix ./modules/profiles/hetzner-cloud-cx21.nix
# The users for this host. # The users for this host.
(lib.private.getUser "nixos" "admin") (foodogsquaredLib.getUser "nixos" "admin")
(lib.private.getUser "nixos" "plover") (foodogsquaredLib.getUser "nixos" "plover")
"${foodogsquaredModulesPath}/profiles/headless.nix" "${foodogsquaredModulesPath}/profiles/headless.nix"
"${foodogsquaredModulesPath}/profiles/hardened.nix" "${foodogsquaredModulesPath}/profiles/hardened.nix"
@ -45,7 +45,7 @@
# Offline SSH!?! # Offline SSH!?!
programs.mosh.enable = true; programs.mosh.enable = true;
sops.secrets = lib.private.getSecrets ./secrets/secrets.yaml { sops.secrets = foodogsquaredLib.sops-nix.getSecrets ./secrets/secrets.yaml {
"ssh-key" = { }; "ssh-key" = { };
"lego/env" = { }; "lego/env" = { };
}; };

View File

@ -1,4 +1,4 @@
{ config, lib, pkgs, ... }: { config, lib, pkgs, foodogsquaredLib, ... }:
let let
hostCfg = config.hosts.plover; hostCfg = config.hosts.plover;
@ -49,7 +49,7 @@ in
lib.mkEnableOption "backup service"; lib.mkEnableOption "backup service";
config = lib.mkIf cfg.enable { config = lib.mkIf cfg.enable {
sops.secrets = lib.private.getSecrets ../../secrets/secrets.yaml { sops.secrets = foodogsquaredLib.sops-nix.getSecrets ../../secrets/secrets.yaml {
"borg/repos/host/patterns/keys" = { }; "borg/repos/host/patterns/keys" = { };
"borg/repos/host/password" = { }; "borg/repos/host/password" = { };
"borg/repos/services/password" = { }; "borg/repos/services/password" = { };

View File

@ -1,7 +1,7 @@
# The DNS server for my domains. Take note it uses a hidden master setup with # The DNS server for my domains. Take note it uses a hidden master setup with
# the secondary nameservers of the service (as of 2023-10-05, we're using # the secondary nameservers of the service (as of 2023-10-05, we're using
# Hetzner's secondary nameservers). # Hetzner's secondary nameservers).
{ config, lib, pkgs, ... }: { config, lib, pkgs, foodogsquaredLib, ... }:
let let
hostCfg = config.hosts.plover; hostCfg = config.hosts.plover;
@ -46,7 +46,7 @@ in
mode = "0400"; mode = "0400";
}; };
in in
lib.private.getSecrets ../../secrets/secrets.yaml { foodogsquaredLib.sops-nix.getSecrets ../../secrets/secrets.yaml {
"dns/${domain}/mailbox-security-key" = dnsFileAttribute; "dns/${domain}/mailbox-security-key" = dnsFileAttribute;
"dns/${domain}/mailbox-security-key-record" = dnsFileAttribute; "dns/${domain}/mailbox-security-key-record" = dnsFileAttribute;
"dns/${domain}/keybase-verification-key" = dnsFileAttribute; "dns/${domain}/keybase-verification-key" = dnsFileAttribute;

View File

@ -2,7 +2,7 @@
# feature in particular to see how this plays out. It might not be toppling # feature in particular to see how this plays out. It might not be toppling
# over the popular services but it is interesting to see new spaces for this # over the popular services but it is interesting to see new spaces for this
# one. # one.
{ config, lib, pkgs, ... }: { config, lib, pkgs, foodogsquaredLib, ... }:
let let
hostCfg = config.hosts.plover; hostCfg = config.hosts.plover;
@ -18,7 +18,7 @@ in
config = lib.mkIf cfg.enable (lib.mkMerge [ config = lib.mkIf cfg.enable (lib.mkMerge [
{ {
sops.secrets = lib.private.getSecrets ../../secrets/secrets.yaml { sops.secrets = foodogsquaredLib.sops-nix.getSecrets ../../secrets/secrets.yaml {
"gitea/db/password".owner = giteaUser; "gitea/db/password".owner = giteaUser;
"gitea/smtp/password".owner = giteaUser; "gitea/smtp/password".owner = giteaUser;
}; };

View File

@ -1,4 +1,4 @@
{ config, lib, pkgs, ... }: { config, lib, pkgs, foodogsquaredLib, ... }:
let let
hostCfg = config.hosts.plover; hostCfg = config.hosts.plover;
@ -28,7 +28,7 @@ in
mode = "0400"; mode = "0400";
}; };
in in
lib.private.getSecrets ../../secrets/secrets.yaml { foodogsquaredLib.sops-nix.getSecrets ../../secrets/secrets.yaml {
"grafana/database/password" = grafanaFileAttributes; "grafana/database/password" = grafanaFileAttributes;
"grafana/users/admin/password" = grafanaFileAttributes; "grafana/users/admin/password" = grafanaFileAttributes;
}; };

View File

@ -1,6 +1,6 @@
# An alternative implementation of Bitwarden written in Rust. The project # An alternative implementation of Bitwarden written in Rust. The project
# being written in Rust is a insta-self-hosting material right there. # being written in Rust is a insta-self-hosting material right there.
{ config, lib, pkgs, ... }: { config, lib, pkgs, foodogsquaredLib, ... }:
let let
hostCfg = config.hosts.plover; hostCfg = config.hosts.plover;
@ -20,7 +20,7 @@ in
config = lib.mkIf cfg.enable (lib.mkMerge [ config = lib.mkIf cfg.enable (lib.mkMerge [
{ {
sops.secrets = lib.private.getSecrets ../../secrets/secrets.yaml { sops.secrets = foodogsquaredLib.sops-nix.getSecrets ../../secrets/secrets.yaml {
"vaultwarden/env".owner = vaultwardenUser; "vaultwarden/env".owner = vaultwardenUser;
}; };

View File

@ -1,4 +1,4 @@
{ config, lib, pkgs, ... }: { config, lib, pkgs, foodogsquaredLib, ... }:
let let
hostCfg = config.hosts.plover; hostCfg = config.hosts.plover;
@ -22,7 +22,7 @@ in
mode = "0400"; mode = "0400";
}; };
in in
lib.private.getSecrets ../../secrets/secrets.yaml { foodogsquaredLib.sops-nix.getSecrets ../../secrets/secrets.yaml {
"vouch-proxy/jwt/secret" = vouchPermissions; "vouch-proxy/jwt/secret" = vouchPermissions;
"vouch-proxy/client/secret" = vouchPermissions; "vouch-proxy/client/secret" = vouchPermissions;
}; };

View File

@ -1,4 +1,4 @@
{ config, lib, pkgs, ... }: { config, lib, pkgs, foodogsquaredLib, ... }:
# Take note this service is heavily based on the hardware networking setup of # Take note this service is heavily based on the hardware networking setup of
# this host so better stay focused on the hardware configuration on this host. # this host so better stay focused on the hardware configuration on this host.
@ -29,7 +29,7 @@ in
mode = "0640"; mode = "0640";
}; };
in in
lib.private.getSecrets ../../secrets/secrets.yaml { foodogsquaredLib.sops-nix.getSecrets ../../secrets/secrets.yaml {
"wireguard/private-key" = systemdNetworkdPermission; "wireguard/private-key" = systemdNetworkdPermission;
"wireguard/preshared-keys/ni" = systemdNetworkdPermission; "wireguard/preshared-keys/ni" = systemdNetworkdPermission;
"wireguard/preshared-keys/phone" = systemdNetworkdPermission; "wireguard/preshared-keys/phone" = systemdNetworkdPermission;

View File

@ -1,10 +1,10 @@
{ config, lib, pkgs, modulesPath, ... }: { config, lib, pkgs, modulesPath, foodogsquaredLib, ... }:
{ {
imports = [ imports = [
"${modulesPath}/profiles/minimal.nix" "${modulesPath}/profiles/minimal.nix"
(lib.private.mapHomeManagerUser "winnow" { (foodogsquaredLib.mapHomeManagerUser "winnow" {
extraGroups = [ extraGroups = [
"wheel" "wheel"
"docker" "docker"

View File

@ -1,4 +1,4 @@
# All of the custom functions used for this configuration. # All of the custom functions suitable for all environments.
{ lib }: { lib }:
rec { rec {
@ -15,52 +15,7 @@ rec {
lib.count (attr: pred attr.name attr.value) lib.count (attr: pred attr.name attr.value)
(lib.mapAttrsToList lib.nameValuePair attrs); (lib.mapAttrsToList lib.nameValuePair attrs);
/* Get the secrets from a given sops file. This will set the individual getConfig = type: config: ../configs/${type}/${config};
attributes `sopsFile` with the given file to not interrupt as much as
possible with your own sops-nix workflow.
Examples: getUser = type: user: ../configs/${type}/_users/${user};
lib.getSecrets ./sops.yaml {
ssh-key = { };
"borg/ssh-key" = { };
"wireguard/private-key" = {
group = config.users.users.systemd-network.group;
reloadUnits = [ "systemd-networkd.service" ];
mode = "0640";
};
}
*/
getSecrets = sopsFile: secrets:
let
getKey = key: { inherit key sopsFile; };
in
lib.mapAttrs
(path: attrs:
(getKey path) // attrs)
secrets;
/* Prepend a prefix for the given secrets. This allows a workflow for
separate sops file.
Examples:
lib.getSecrets ./sops.yaml {
ssh-key = { };
"borg/ssh-key" = { };
} //
(lib.getSecrets ./wireguard.yaml
(lib.attachSopsPathPrefix "wireguard" {
"private-key" = {
group = config.users.users.systemd-network.group;
reloadUnits = [ "systemd-networkd.service" ];
mode = "0640";
};
}))
*/
attachSopsPathPrefix = prefix: secrets:
lib.mapAttrs'
(key: settings:
lib.nameValuePair
"${prefix}/${key}"
({ inherit key; } // settings))
secrets;
} }

View File

@ -1,16 +0,0 @@
# A lambda suitable to be used for `pkgs.lib.extend`.
self: super:
let
publicLib = import ../. { lib = super; };
in
{
inherit (publicLib) countAttrs getSecrets attachSopsPathPrefix;
# Until I figure out how to properly add them only for their respective
# environment, this is the working solution for now. Not really perfect
# since we use one nixpkgs instance for each configuration (home-manager or
# otherwise).
private = publicLib
// import ../private.nix { lib = self; }
// import ../home-manager.nix { lib = self; };
}

View File

@ -0,0 +1,6 @@
# The home-manager set.
{ lib }:
import ../default.nix { inherit lib; }
// import ../home-manager.nix { inherit lib; }
// { sops-nix = import ../sops.nix { inherit lib; }; }

6
lib/extras/nixos-set.nix Normal file
View File

@ -0,0 +1,6 @@
# The NixOS library set.
{ lib }:
import ../default.nix { inherit lib; }
// import ../home-manager.nix { inherit lib; }
// { sops-nix = import ../sops.nix { inherit lib; }; }

View File

@ -0,0 +1,4 @@
# NixVim library set.
{ lib }:
import ../default.nix { inherit lib; }

View File

@ -1,5 +1,4 @@
# This is just a library intended solely for this flake. # All of the functions suitable only for NixOS.
# It is expected to use the nixpkgs library with `lib/default.nix`.
{ lib }: { lib }:
rec { rec {
@ -22,7 +21,7 @@ rec {
home.homeDirectory = homeDirectory; home.homeDirectory = homeDirectory;
} }
(getConfig "home-manager" user) ../configs/home-manager/${user}
]; ];
}; };
@ -31,8 +30,4 @@ rec {
settings settings
]; ];
}); });
getConfig = type: config: ../configs/${type}/${config};
getUser = type: user: ../configs/${type}/_users/${user};
} }

53
lib/sops.nix Normal file
View File

@ -0,0 +1,53 @@
# A library specifically for environments with sops-nix.
{ lib }:
{
/* Get the secrets from a given sops file. This will set the individual
attributes `sopsFile` with the given file to not interrupt as much as
possible with your own sops-nix workflow.
Examples:
lib.getSecrets ./sops.yaml {
ssh-key = { };
"borg/ssh-key" = { };
"wireguard/private-key" = {
group = config.users.users.systemd-network.group;
reloadUnits = [ "systemd-networkd.service" ];
mode = "0640";
};
}
*/
getSecrets = sopsFile: secrets:
let
getKey = key: { inherit key sopsFile; };
in
lib.mapAttrs
(path: attrs:
(getKey path) // attrs)
secrets;
/* Prepend a prefix for the given secrets. This allows a workflow for
separate sops file.
Examples:
lib.getSecrets ./sops.yaml {
ssh-key = { };
"borg/ssh-key" = { };
} //
(lib.getSecrets ./wireguard.yaml
(lib.attachSopsPathPrefix "wireguard" {
"private-key" = {
group = config.users.users.systemd-network.group;
reloadUnits = [ "systemd-networkd.service" ];
mode = "0640";
};
}))
*/
attachSopsPathPrefix = prefix: secrets:
lib.mapAttrs'
(key: settings:
lib.nameValuePair
"${prefix}/${key}"
({ inherit key; } // settings))
secrets;
}

View File

@ -22,7 +22,7 @@ let
}; };
inherit pkgs; inherit pkgs;
lib = pkgs.lib.extend (import ../../../lib/extras/extend-lib.nix); lib = pkgs.lib;
modules = extraModules; modules = extraModules;
}; };
@ -236,6 +236,11 @@ in
setups.home-manager.sharedModules = [ setups.home-manager.sharedModules = [
homeManagerModules homeManagerModules
({ lib, ... }: {
_module.args.foodogsquaredLib =
import ../../../lib/extras/home-manager-set.nix { inherit lib; };
})
# Import our private modules... # Import our private modules...
../../home-manager/_private ../../home-manager/_private
]; ];

View File

@ -18,7 +18,7 @@ let
nixpkgs = inputs.${nixpkgsBranch}; nixpkgs = inputs.${nixpkgsBranch};
# Just to be sure, we'll use everything with the given nixpkgs' stdlib. # Just to be sure, we'll use everything with the given nixpkgs' stdlib.
lib' = nixpkgs.lib.extend (import ../../../lib/extras/extend-lib.nix); lib = nixpkgs.lib;
# A modified version of `nixosSystem` from nixpkgs flake. There is a # A modified version of `nixosSystem` from nixpkgs flake. There is a
# recent change at nixpkgs (at 039f73f134546e59ec6f1b56b4aff5b81d889f64) # recent change at nixpkgs (at 039f73f134546e59ec6f1b56b4aff5b81d889f64)
@ -26,11 +26,10 @@ let
# evaluate the NixOS system ourselves. # evaluate the NixOS system ourselves.
nixosSystem = args: import "${nixpkgs}/nixos/lib/eval-config.nix" args; nixosSystem = args: import "${nixpkgs}/nixos/lib/eval-config.nix" args;
in in
(lib'.makeOverridable nixosSystem) { (lib.makeOverridable nixosSystem) {
specialArgs = { specialArgs = {
foodogsquaredModulesPath = builtins.toString nixosModules; foodogsquaredModulesPath = builtins.toString nixosModules;
}; };
lib = lib';
modules = extraModules ++ [{ modules = extraModules ++ [{
nixpkgs.hostPlatform = lib.mkForce system; nixpkgs.hostPlatform = lib.mkForce system;
}]; }];
@ -515,6 +514,11 @@ in
# Import our private modules. # Import our private modules.
../../nixos/_private ../../nixos/_private
({ lib, ... }: {
_module.args.foodogsquaredLib =
import ../../../lib/extras/nixos-set.nix { inherit lib; };
})
# Set the home-manager-related settings. # Set the home-manager-related settings.
({ lib, ... }: { ({ lib, ... }: {
home-manager.sharedModules = partsConfig.setups.home-manager.sharedModules; home-manager.sharedModules = partsConfig.setups.home-manager.sharedModules;

View File

@ -102,7 +102,14 @@ in
}; };
config = lib.mkIf (cfg.configs != { }) { config = lib.mkIf (cfg.configs != { }) {
setups.nixvim.sharedModules = [ nixvimModules ]; setups.nixvim.sharedModules = [
nixvimModules
({ lib, ... }: {
_module.args.foodogsquaredLib =
import ../../../lib/extras/nixvim-set.nix { inherit lib; };
})
];
perSystem = { system, config, lib, ... }: perSystem = { system, config, lib, ... }:
( (

View File

@ -23,7 +23,11 @@ in {
config = { config = {
assertions = [{ assertions = [{
assertion = assertion =
let enabledThemes = lib.countAttrs (_: theme: theme.enable) cfg.workflows; let
countAttrs = pred: attrs:
lib.count (attr: pred attr.name attr.value)
(lib.mapAttrsToList lib.nameValuePair attrs);
enabledThemes = countAttrs (_: theme: theme.enable) cfg.workflows;
in cfg.disableLimit || (enabledThemes <= 1); in cfg.disableLimit || (enabledThemes <= 1);
message = "Can't have more than one theme enabled at any given time."; message = "Can't have more than one theme enabled at any given time.";
}]; }];