cerulean/flake.nix

180 lines
6 KiB
Nix
Raw Normal View History

2025-12-14 14:02:06 +10:00
# Copyright 2025 Emile Clark-Boman
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
2025-12-13 09:30:20 +10:00
{
description = "Your Nix Cloud Simplified";
inputs = {
2025-12-13 13:24:37 +10:00
systems.url = "github:nix-systems/default";
2025-12-13 09:30:20 +10:00
nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.11";
nixpkgs-unstable.url = "github:NixOS/nixpkgs/nixos-unstable";
2025-12-13 13:24:37 +10:00
nib = {
url = "github:emilelcb/nib";
inputs.systems.follows = "systems";
2025-12-13 13:24:37 +10:00
};
deploy-rs.url = "github:serokell/deploy-rs";
2025-12-13 09:30:20 +10:00
};
2025-12-13 13:24:37 +10:00
outputs = {
2025-12-13 09:30:20 +10:00
self,
nixpkgs,
2025-12-13 13:24:37 +10:00
nixpkgs-unstable,
nib,
deploy-rs,
2025-12-13 09:30:20 +10:00
...
2025-12-14 13:25:00 +10:00
} @ inputs:
2025-12-14 18:56:29 +10:00
with builtins;
with nib.types; let
2025-12-14 13:25:00 +10:00
lib = nixpkgs.lib;
sys = with nib;
mkUSys {
pkgs = withPkgs nixpkgs {
config.allowUnfree = false;
overlays = attrValues self.overlays;
};
upkgs = withPkgs nixpkgs-unstable {
config.allowUnfree = false;
};
2025-12-14 10:34:53 +10:00
};
2025-12-14 13:25:00 +10:00
in rec {
overlays = [
# deploy-rs is built from the flake input, not from nixpkgs!
# To take advantage of the nixpkgs binary cache,
# the deploy-rs package can be overwritten:
deploy-rs.overlays.default
(self: super: {
deploy-rs = {
inherit (super) deploy-rs;
lib = super.deploy-rs.lib;
};
})
];
mkNexusConfig = config: let
# abstract node instance that stores all default values
templateNode = name: system: let
missing = msg: path:
2025-12-14 18:56:29 +10:00
Terminal (abort ''
2025-12-14 13:25:00 +10:00
Each Cerulean Nexus node is required to specify ${msg}!
Ensure `cerulean.nexus.nodes.${name}.${path}` exists under your call to `cerulean.mkNexus`.
2025-12-14 18:56:29 +10:00
'');
2025-12-14 13:25:00 +10:00
in {
system = missing "its system type" "system"; # intentionally left missing!! (to raise errors)
modules = missing "its required modules" "modules";
specialArgs = {
inherit inputs;
2025-12-14 18:56:29 +10:00
pkgs = Terminal (sys.pkgsFor system);
upkgs = Terminal (sys.upkgsFor system);
2025-12-14 13:25:00 +10:00
};
2025-12-14 13:25:00 +10:00
deploy = {
user = "root";
sudo = "sudo -u";
interactiveSudo = false;
2025-12-14 13:25:00 +10:00
remoteBuild = false; # prefer local builds for remote deploys
2025-12-14 13:25:00 +10:00
autoRollback = true; # reactivate previous profile if activation fails
magicRollback = true;
2025-12-14 13:25:00 +10:00
activationTimeout = 500; # timeout in seconds for profile activation
confirmTimeout = 30; # timeout in seconds for profile activation confirmation
2025-12-14 13:25:00 +10:00
ssh = {
host = missing "an SSH hostname (domain name or ip address) for deployment" "deploy.ssh.host";
user = missing "an SSH username for deployment" "deploy.ssh.user";
port = 22;
opts = [];
};
};
};
2025-12-14 13:25:00 +10:00
parseNode = name: nodeAttrs:
if !(isAttrs nodeAttrs)
then
# fail if node is not an attribute set
abort ''
Cerulean Nexus nodes must be provided as an attribute set, got "${typeOf nodeAttrs}" instead!
Ensure all `cerulean.nexus.nodes.${name}` declarations are attribute sets under your call to `cerulean.mkNexus`.
''
# TODO: nodeAttrs.system won't display any nice error messages!!
# TODO: will mergeTypedStruct give nice error messages? or should I use mergeStructErr directly?
else let
templateAttrs = templateNode name nodeAttrs.system;
2025-12-14 18:56:29 +10:00
S = nib.parse.parseStructFor templateAttrs nodeAttrs;
2025-12-14 13:25:00 +10:00
in
2025-12-14 18:56:29 +10:00
unwrapRes (_:
2025-12-14 13:25:00 +10:00
abort ''
Cerulean failed to parse `cerulean.nexus.nodes.${name}`!
mergeStruct should never return `result.Err`... How are you here?!?
'')
2025-12-14 18:56:29 +10:00
S;
2025-12-14 13:25:00 +10:00
# TODO: mapNodes = f: mapAttrs (name: nodeAttrs: f name (parseNode name nodeAttrs)) config.nexus.nodes
mapNodes = f: mapAttrs f (mapAttrs parseNode config.nexus.nodes);
in rec {
nixosConfigurations = mapNodes (
_: node:
lib.nixosSystem {
system = node.system;
modules = node.modules;
# nix passes these to every single module
specialArgs = {} // node.modules.specialArgs;
}
);
deploy.nodes = mapNodes (_: node: {
hostname = node.deploy.ssh.host;
profilesOrder = ["default"]; # profiles priority
profiles.default = {
path = deploy-rs.lib.${node.system}.activate.nixos nixosConfigurations.${node.system};
user = node.deploy.user;
sudo = node.deploy.sudo;
interactiveSudo = node.deploy.interactiveSudo;
fastConnection = false;
autoRollback = node.deploy.autoRollback;
magicRollback = node.deploy.magicRollback;
activationTimeout = node.deploy.activationTimeout;
confirmTimeout = node.deploy.confirmTimeout;
remoteBuild = node.deploy.remoteBuild;
sshUser = node.deploy.ssh.user;
sshOpts =
node.deploy.ssh.opts
++ (
if elem "-p" node.deploy.ssh.opts
then []
else ["-p" (toString node.deploy.ssh.port)]
);
};
});
2025-12-13 13:24:37 +10:00
2025-12-14 13:25:00 +10:00
checks = mapAttrs (system: deployLib: deployLib.deployChecks deploy) deploy-rs.lib;
};
2025-12-13 13:24:37 +10:00
2025-12-14 13:25:00 +10:00
mkNexus = outputs: let
config = outputs.cerulean;
in
(mkNexusConfig config) // (removeAttrs outputs ["cerulean"]);
};
2025-12-13 09:30:20 +10:00
}