Skip to content

Commit

Permalink
Add switch action to nixidy cli
Browse files Browse the repository at this point in the history
  • Loading branch information
arnarg committed Jun 28, 2024
1 parent 79bcd51 commit b93a2c1
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
32 changes: 32 additions & 0 deletions modules/build.nix
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ in {
internal = true;
description = "The package containing all the applications for an environment.";
};
build.activationPackage = mkOption {
type = types.package;
internal = true;
description = "The package containing all the applications and an activation script.";
};
};

config = {
Expand Down Expand Up @@ -90,5 +95,32 @@ in {
config.build.extrasPackage
];
};

build.activationPackage = pkgs.stdenv.mkDerivation {
name = "nixidy-activation-environment-${envName}";
phases = ["installPhase"];

installPhase = ''
mkdir -p $out
ln -s ${config.build.environmentPackage} $out/environment
cat <<EOF > $out/activate
#!/usr/bin/env bash
set -eo pipefail
dest="${config.nixidy.target.rootPath}"
mkdir -p "\$dest"
echo "switching manifests"
${pkgs.rsync}/bin/rsync --recursive --delete -L "${config.build.environmentPackage}/" "\$dest"
echo "done!"
EOF
chmod +x $out/activate
'';
};
};
}
1 change: 1 addition & 0 deletions modules/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ in {
branch = module.config.nixidy.target.branch;
};
environmentPackage = module.config.build.environmentPackage;
activationPackage = module.config.build.activationPackage;
}
21 changes: 20 additions & 1 deletion nixidy/nixidy
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ function doBuild() {
nix build "${FLAKE_ROOT}#nixidyEnvs.${NIX_SYSTEM}.${FLAKE_ENV}.environmentPackage" "${BUILD_PARAMS[@]}"
}

function doSwitch() {
setFlakeParam

if [[ -z "$FLAKE_ENV" ]]; then
doHelp
exit 1
fi

ENVIRON=$(nix build "${FLAKE_ROOT}#nixidyEnvs.${NIX_SYSTEM}.${FLAKE_ENV}.activationPackage" --no-link --print-out-paths)

"${ENVIRON}/activate"
}

function doHelp() {
echo "Usage: $0 [OPTION] COMMAND"
echo
Expand All @@ -70,6 +83,9 @@ function doHelp() {
echo
echo " build FLAKE_URI Build nixidy environment from flake URI."
echo " Example: .#prod"
echo
echo " switch FLAKE_URI Build and switch to nixidy environment from flake URI."
echo " Example: .#prod"
}

COMMAND=""
Expand All @@ -82,7 +98,7 @@ while [[ $# -gt 0 ]]; do
opt="$1"
shift
case $opt in
build|info|help)
build|switch|info|help)
COMMAND="$opt"
;;
--no-link)
Expand Down Expand Up @@ -126,6 +142,9 @@ case $COMMAND in
build)
doBuild
;;
switch)
doSwitch
;;
help)
doHelp
;;
Expand Down

0 comments on commit b93a2c1

Please sign in to comment.