deploy-rs/flake.nix

104 lines
3.7 KiB
Nix
Raw Normal View History

# SPDX-FileCopyrightText: 2020 Serokell <https://serokell.io/>
#
# 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 { };
isDarwin = pkgs.lib.strings.hasSuffix "-darwin" system;
darwinOptions = pkgs.lib.optionalAttrs isDarwin {
nativeBuildInputs = [
pkgs.darwin.apple_sdk.frameworks.SystemConfiguration
];
singleStep = true; # https://github.com/nmattia/naersk/issues/127
cargoBuildOptions = opts: opts ++ [ "--bin" "deploy" ]; # The "activate" binary is linux-only.
};
in
{
2020-10-09 06:01:01 +03:00
defaultPackage = self.packages."${system}".deploy-rs;
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";
program = "${self.defaultPackage."${system}"}/bin/deploy";
2020-09-28 20:48:21 +03:00
};
2020-11-16 14:49:00 +03:00
devShell = pkgs.mkShell {
inputsFrom = [ self.packages.${system}.deploy-rs ];
buildInputs = [ pkgs.nixUnstable ];
};
2020-11-14 23:37:51 +03:00
lib = rec {
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}
${activate}
'';
executable = true;
destination = "/deploy-rs-activate";
})
];
};
nixos = base: custom base.config.system.build.toplevel "$PROFILE/bin/switch-to-configuration switch";
noop = base: custom base ":";
};
2020-11-15 05:46:19 +03:00
deployChecks = deploy: builtins.mapAttrs (_: check: check deploy) {
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
'';
activate = deploy:
let
profiles = builtins.concatLists (pkgs.lib.mapAttrsToList (nodeName: node: pkgs.lib.mapAttrsToList (profileName: profile: [ (toString profile.path) nodeName profileName ]) node.profiles) deploy.nodes);
in
pkgs.runCommandNoCC "deploy-rs-check-activate" { } ''
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:)
test -f "$profile_path/deploy-rs-activate" || (echo "#$node_name.$profile_name is missing an activation script" && exit 1);
done
touch $out
'';
};
};
2020-09-28 20:48:21 +03:00
});
}