2020-09-30 01:10:06 +03:00
|
|
|
# SPDX-FileCopyrightText: 2020 Serokell <https://serokell.io/>
|
2020-12-02 06:41:22 +03:00
|
|
|
# SPDX-FileCopyrightText: 2020 Andreas Fuchs <asf@boinkor.net>
|
2020-09-30 01:10:06 +03:00
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2020-09-28 20:48:21 +03:00
|
|
|
{
|
2020-10-09 06:01:01 +03:00
|
|
|
description = "A Simple multi-profile Nix-flake deploy tool.";
|
|
|
|
|
2020-09-28 20:48:21 +03:00
|
|
|
inputs = {
|
|
|
|
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
|
2020-10-09 06:01:01 +03:00
|
|
|
naersk = {
|
|
|
|
url = "github:nmattia/naersk/master";
|
|
|
|
inputs.nixpkgs.follows = "nixpkgs";
|
|
|
|
};
|
2020-09-28 20:48:21 +03:00
|
|
|
utils.url = "github:numtide/flake-utils";
|
2020-10-09 06:01:01 +03:00
|
|
|
flake-compat = {
|
|
|
|
url = "github:edolstra/flake-compat";
|
|
|
|
flake = false;
|
|
|
|
};
|
2020-09-28 20:48:21 +03:00
|
|
|
};
|
|
|
|
|
2020-10-09 06:01:01 +03:00
|
|
|
outputs = { self, nixpkgs, utils, naersk, ... }:
|
2020-09-28 20:48:21 +03:00
|
|
|
utils.lib.eachDefaultSystem (system:
|
|
|
|
let
|
|
|
|
pkgs = import nixpkgs { inherit system; };
|
|
|
|
naersk-lib = pkgs.callPackage naersk { };
|
2020-11-25 08:11:15 +03:00
|
|
|
isDarwin = pkgs.lib.strings.hasSuffix "-darwin" system;
|
|
|
|
darwinOptions = pkgs.lib.optionalAttrs isDarwin {
|
|
|
|
nativeBuildInputs = [
|
|
|
|
pkgs.darwin.apple_sdk.frameworks.SystemConfiguration
|
|
|
|
];
|
|
|
|
};
|
2020-10-06 05:46:28 +03:00
|
|
|
in
|
|
|
|
{
|
2020-10-09 06:01:01 +03:00
|
|
|
defaultPackage = self.packages."${system}".deploy-rs;
|
2020-11-25 08:11:15 +03:00
|
|
|
packages.deploy-rs = naersk-lib.buildPackage (darwinOptions // {
|
|
|
|
root = ./.;
|
|
|
|
});
|
2020-09-28 20:48:21 +03:00
|
|
|
|
2020-10-09 06:01:01 +03:00
|
|
|
defaultApp = self.apps."${system}".deploy-rs;
|
|
|
|
apps.deploy-rs = {
|
2020-09-28 20:48:21 +03:00
|
|
|
type = "app";
|
2020-09-29 01:45:53 +03:00
|
|
|
program = "${self.defaultPackage."${system}"}/bin/deploy";
|
2020-09-28 20:48:21 +03:00
|
|
|
};
|
2020-10-06 05:46:28 +03:00
|
|
|
|
2020-11-16 14:49:00 +03:00
|
|
|
devShell = pkgs.mkShell {
|
2020-11-25 08:11:15 +03:00
|
|
|
inputsFrom = [ self.packages.${system}.deploy-rs ];
|
|
|
|
buildInputs = [ pkgs.nixUnstable ];
|
|
|
|
};
|
2020-11-14 23:37:51 +03:00
|
|
|
|
2021-01-26 14:51:08 +03:00
|
|
|
checks = {
|
|
|
|
deploy-rs = self.defaultPackage.${system}.overrideAttrs (super: { doCheck = true; });
|
|
|
|
};
|
|
|
|
|
2020-10-26 21:29:01 +03:00
|
|
|
lib = rec {
|
2020-10-06 05:46:28 +03:00
|
|
|
|
2020-11-15 22:30:45 +03:00
|
|
|
setActivate = builtins.trace
|
|
|
|
"deploy-rs#lib.setActivate is deprecated, use activate.noop, activate.nixos or activate.custom instead"
|
|
|
|
activate.custom;
|
|
|
|
|
|
|
|
activate = rec {
|
|
|
|
custom = base: activate: pkgs.buildEnv {
|
|
|
|
name = ("activatable-" + base.name);
|
|
|
|
paths = [
|
|
|
|
base
|
|
|
|
(pkgs.writeTextFile {
|
|
|
|
name = base.name + "-activate-path";
|
|
|
|
text = ''
|
|
|
|
#!${pkgs.runtimeShell}
|
2021-01-23 02:26:11 +03:00
|
|
|
set -euo pipefail
|
|
|
|
|
2020-11-15 22:30:45 +03:00
|
|
|
${activate}
|
|
|
|
'';
|
|
|
|
executable = true;
|
|
|
|
destination = "/deploy-rs-activate";
|
|
|
|
})
|
2020-11-26 03:00:01 +03:00
|
|
|
(pkgs.writeTextFile {
|
|
|
|
name = base.name + "-activate-rs";
|
|
|
|
text = ''
|
|
|
|
#!${pkgs.runtimeShell}
|
|
|
|
exec ${self.defaultPackage."${system}"}/bin/activate "$@"
|
|
|
|
'';
|
|
|
|
executable = true;
|
|
|
|
destination = "/activate-rs";
|
|
|
|
})
|
2020-11-15 22:30:45 +03:00
|
|
|
];
|
|
|
|
};
|
2020-11-15 20:29:43 +03:00
|
|
|
|
2021-01-06 02:44:00 +03:00
|
|
|
nixos = base: custom base.config.system.build.toplevel ''
|
|
|
|
$PROFILE/bin/switch-to-configuration switch
|
|
|
|
|
|
|
|
# https://github.com/serokell/deploy-rs/issues/31
|
2021-01-09 00:45:10 +03:00
|
|
|
${with base.config.boot.loader;
|
|
|
|
pkgs.lib.optionalString systemd-boot.enable
|
|
|
|
"sed -i '/^default /d' ${efi.efiSysMountPoint}/loader/loader.conf"}
|
2021-01-06 02:44:00 +03:00
|
|
|
'';
|
2020-11-15 22:30:45 +03:00
|
|
|
|
|
|
|
noop = base: custom base ":";
|
|
|
|
};
|
2020-11-15 20:29:43 +03:00
|
|
|
|
2020-11-15 05:46:19 +03:00
|
|
|
deployChecks = deploy: builtins.mapAttrs (_: check: check deploy) {
|
2020-10-26 21:29:01 +03:00
|
|
|
schema = deploy: pkgs.runCommandNoCC "jsonschema-deploy-system" { } ''
|
2020-11-07 21:56:22 +03:00
|
|
|
${pkgs.python3.pkgs.jsonschema}/bin/jsonschema -i ${pkgs.writeText "deploy.json" (builtins.toJSON deploy)} ${./interface.json} && touch $out
|
2020-10-26 21:29:01 +03:00
|
|
|
'';
|
|
|
|
|
|
|
|
activate = deploy:
|
|
|
|
let
|
2020-10-31 05:11:16 +03:00
|
|
|
profiles = builtins.concatLists (pkgs.lib.mapAttrsToList (nodeName: node: pkgs.lib.mapAttrsToList (profileName: profile: [ (toString profile.path) nodeName profileName ]) node.profiles) deploy.nodes);
|
2020-10-26 21:29:01 +03:00
|
|
|
in
|
|
|
|
pkgs.runCommandNoCC "deploy-rs-check-activate" { } ''
|
2020-10-31 05:11:16 +03:00
|
|
|
for x in ${builtins.concatStringsSep " " (map (p: builtins.concatStringsSep ":" p) profiles)}; do
|
|
|
|
profile_path=$(echo $x | cut -f1 -d:)
|
|
|
|
node_name=$(echo $x | cut -f2 -d:)
|
|
|
|
profile_name=$(echo $x | cut -f3 -d:)
|
|
|
|
|
2020-11-27 19:53:26 +03:00
|
|
|
test -f "$profile_path/deploy-rs-activate" || (echo "#$node_name.$profile_name is missing the deploy-rs-activate activation script" && exit 1);
|
|
|
|
|
|
|
|
test -f "$profile_path/activate-rs" || (echo "#$node_name.$profile_name is missing the activate-rs activation script" && exit 1);
|
2020-10-31 05:11:16 +03:00
|
|
|
done
|
2020-10-26 21:29:01 +03:00
|
|
|
|
|
|
|
touch $out
|
|
|
|
'';
|
|
|
|
};
|
2020-10-06 05:46:28 +03:00
|
|
|
};
|
2020-09-28 20:48:21 +03:00
|
|
|
});
|
|
|
|
}
|