Initial commit

This commit is contained in:
Felix Bargfeldt 2023-08-26 17:21:03 +02:00
commit afac393ba7
Signed by: Defelo
GPG key ID: 2A05272471204DD3
3 changed files with 98 additions and 0 deletions

21
LICENSE Normal file
View file

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2023 Felix
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

2
README.md Normal file
View file

@ -0,0 +1,2 @@
# deploy-sh
Very simple NixOS remote deployment tool

75
flake.nix Normal file
View file

@ -0,0 +1,75 @@
{
outputs = {self}: {
lib.mkDeploy = {
hosts,
pkgs,
}: let
inherit (builtins) concatStringsSep;
inherit (pkgs.lib) mapAttrsToList escapeShellArg optionalString;
mkDeploy = command:
pkgs.writeShellScript "deploy-${command}" ''
export PATH=${pkgs.nixos-rebuild}/bin:$PATH
declare -A deploy_commands=(
${concatStringsSep "\n" (
mapAttrsToList (k: v: let
cfg = v.config.deploy;
cmd = "nixos-rebuild ${command} --flake ${escapeShellArg ".#${k}"} --target-host ${escapeShellArg cfg.host} ${optionalString cfg.remoteBuild "--build-host ${escapeShellArg cfg.host}"}";
in " [${escapeShellArg k}]=${escapeShellArg cmd}")
hosts
)}
)
fst=1
deploy() {
[[ $fst = 0 ]] && echo; fst=0
cmd="''${deploy_commands["$1"]}"
if [[ -z "$cmd" ]]; then
echo -e "\033[1m\033[31mHost '$1' not found!\033[0m"
exit 1
else
echo -e "\033[1mDeploying host '$1'\033[0m"
echo -e "+ $cmd"
if eval "$cmd"; then
echo -e "\033[1m\033[32mHost '$1' successfully deployed!\033[0m"
else
echo -e "\033[1m\033[31mDeployment of host '$1' failed!\033[0m"
exit 2
fi
fi
}
if [[ $# -eq 0 ]]; then
${concatStringsSep "\n" (mapAttrsToList (k: v: " deploy ${escapeShellArg k}") hosts)}
else
for host in $@; do
deploy "$host"
done
fi
'';
in
pkgs.stdenv.mkDerivation {
name = "deploy-sh";
unpackPhase = "true";
installPhase = ''
mkdir -p $out/bin;
cp ${mkDeploy "switch"} $out/bin/deploy
cp ${mkDeploy "test"} $out/bin/deploy-test
cp ${mkDeploy "boot"} $out/bin/deploy-boot
'';
};
nixosModules.default = {lib, ...}:
with lib; {
options.deploy = {
host = mkOption {
type = types.str;
};
remoteBuild = mkOption {
type = types.bool;
default = true;
};
};
config = {};
};
};
}