infrastructure/flake.nix

124 lines
4 KiB
Nix
Raw Normal View History

2021-10-13 16:49:41 +02:00
{
description = "Delft Deployment";
# Based on:
# * https://github.com/serokell/pegasus-infra/blob/master/flake.nix
# * https://git.voidcorp.nl/j00lz/nixos-configs/src/branch/main/flake.nix
2021-11-16 21:52:43 +01:00
2021-11-16 16:28:55 +01:00
inputs = {
deploy-rs.url = "github:serokell/deploy-rs";
2022-05-15 01:24:06 +02:00
# nixpkgs.url = "github:nixos/nixpkgs/master";
2022-05-20 10:51:15 +02:00
nixpkgs.url = "github:NULLx76/nixpkgs/0x76";
2021-11-16 16:28:55 +01:00
serokell-nix.url = "github:serokell/serokell.nix";
vault-secrets.url = "github:serokell/vault-secrets";
2022-05-05 15:16:37 +02:00
minecraft-servers.url = "github:jyooru/nix-minecraft-servers";
2021-11-16 16:28:55 +01:00
};
2021-10-13 16:49:41 +02:00
2021-11-17 00:55:01 +01:00
outputs =
{ self, nixpkgs, deploy-rs, vault-secrets, serokell-nix, ... }@inputs:
2021-11-16 21:52:43 +01:00
let
inherit (nixpkgs) lib;
2021-11-21 19:30:19 +01:00
inherit (builtins) filter mapAttrs;
2021-11-16 21:52:43 +01:00
system = "x86_64-linux";
2021-11-21 19:30:19 +01:00
hosts = import ./hosts.nix;
# Create a nixosConfiguration based on a foldername (nixname) and if the host is an LXC container or a VM.
2021-11-21 16:32:30 +01:00
mkConfig = { hostname, profile ? hostname, lxc ? true, ... }: {
"${profile}" = lib.nixosSystem {
2021-11-16 21:52:43 +01:00
inherit system;
2021-12-02 16:34:48 +01:00
modules = [
./nixos/common
"${./.}/nixos/hosts/${profile}/configuration.nix"
] ++ (if lxc then [
"${nixpkgs}/nixos/modules/virtualisation/lxc-container.nix"
./nixos/common/generic-lxc.nix
] else
[ ./nixos/common/generic-vm.nix ]);
2021-11-21 19:30:19 +01:00
specialArgs = { inherit hosts inputs; };
2021-11-16 16:28:55 +01:00
};
2021-10-17 21:02:20 +02:00
};
2021-10-18 18:54:07 +02:00
# Same as above, but for the nodes part of deploy.
2021-11-21 16:32:30 +01:00
mkDeploy = { ip, hostname, profile ? hostname, ... }: {
"${hostname}" = {
hostname = ip;
fastConnection = true;
profiles.system = {
user = "root";
2022-05-08 02:13:44 +02:00
path = deploy-rs.lib.${system}.activate.nixos self.nixosConfigurations.${profile};
2021-11-21 16:32:30 +01:00
};
};
2021-11-21 16:32:30 +01:00
};
2021-10-18 23:26:26 +02:00
# Generates hosts.auto.tfvars.json for Terraform
2022-05-08 02:13:44 +02:00
genTFVars =
let
hostToVar = z@{ hostname, mac, ... }: {
"${hostname}" = { inherit mac; };
};
hostSet = lib.foldr (el: acc: acc // hostToVar el) { } hosts;
json = builtins.toJSON { hosts = hostSet; };
in
pkgs.writeScriptBin "gen-tf-vars" ''
echo '${json}' | ${pkgs.jq}/bin/jq > terraform/hosts.auto.tfvars.json;
echo "Generated Terraform Variables";
'';
# Import all nixos host definitions that are actual nix machines
2021-11-21 19:30:19 +01:00
nixHosts = filter ({ nix ? true, ... }: nix) hosts;
2021-11-22 00:10:21 +01:00
2022-05-08 02:13:44 +02:00
pkgs = serokell-nix.lib.pkgsWith nixpkgs.legacyPackages.${system} [ vault-secrets.overlay ];
2021-11-23 17:44:00 +01:00
2022-05-08 02:13:44 +02:00
deployChecks = mapAttrs (_: lib: lib.deployChecks self.deploy) deploy-rs.lib;
2021-11-24 22:41:26 +01:00
checks = { };
2022-05-08 02:13:44 +02:00
in
{
# Make the config and deploy sets
2022-05-08 02:13:44 +02:00
nixosConfigurations = lib.foldr (el: acc: acc // mkConfig el) { } nixHosts;
2021-11-21 16:32:30 +01:00
deploy.nodes = lib.foldr (el: acc: acc // mkDeploy el) { } nixHosts;
2021-11-03 22:55:03 +01:00
2021-12-02 16:34:48 +01:00
apps.${system} = {
2022-05-08 13:26:27 +02:00
deploy = {
type = "app";
program = "${deploy-rs.packages.${system}.deploy-rs}/bin/deploy";
};
2021-12-02 16:34:48 +01:00
vault-push-approles = {
type = "app";
program = "${pkgs.vault-push-approles self}/bin/vault-push-approles";
};
vault-push-approle-envs = {
type = "app";
program =
"${pkgs.vault-push-approle-envs self}/bin/vault-push-approle-envs";
};
tfvars = {
type = "app";
program = "${genTFVars}/bin/gen-tf-vars";
};
};
2021-11-16 21:52:43 +01:00
# Use by running `nix develop`
2022-04-04 13:46:01 +02:00
devShells.${system}.default = pkgs.mkShell {
2021-11-23 17:44:00 +01:00
VAULT_ADDR = "http://vault.olympus:8200/";
2021-11-21 16:32:30 +01:00
# This only support bash so just execute zsh in bash as a workaround :/
2022-05-08 02:13:44 +02:00
shellHook = "zsh";
buildInputs = with pkgs; [
2021-11-16 16:28:55 +01:00
deploy-rs.packages.${system}.deploy-rs
fluxcd
k9s
kubectl
kubectx
2021-11-21 16:32:30 +01:00
terraform
nixfmt
nixUnstable
vault
2022-05-13 16:31:59 +02:00
# (vault-push-approles self { })
# (vault-push-approle-envs self { })
genTFVars
2021-11-16 16:28:55 +01:00
];
};
2021-11-23 17:44:00 +01:00
checks = lib.recursiveUpdate deployChecks checks;
2021-11-16 16:28:55 +01:00
};
2021-10-13 16:49:41 +02:00
}