Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions deploy_nixos/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ variable "hermetic" {
default = false
}

variable "delete_older_than" {
type = string
description = "Can be a list of generation numbers, the special value old to delete all non-current generations, a value such as 30d to delete all generations older than the specified number of days (except for the generation that was active at that point in time), or a value such as +5 to keep the last 5 generations ignoring any newer than current, e.g., if 30 is the current generation +5 will delete generation 25 and all older generations."
default = "+1"
}

# --------------------------------------------------------------------------

locals {
Expand Down Expand Up @@ -184,6 +190,7 @@ resource "null_resource" "deploy_nixos" {
local.build_on_target,
local.ssh_private_key == "" ? "-" : local.ssh_private_key,
"switch",
var.delete_older_than,
],
local.extra_build_args
)
Expand Down
8 changes: 6 additions & 2 deletions deploy_nixos/nixos-deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ targetPort="$4"
buildOnTarget="$5"
sshPrivateKey="$6"
action="$7"
shift 7
deleteOlderThan="$8"
shift 8

# remove the last argument
set -- "${@:1:$(($# - 1))}"
Expand Down Expand Up @@ -126,4 +127,7 @@ targetHostCmd "$outPath/bin/switch-to-configuration" "$action"

# Cleanup previous generations
log "collecting old nix derivations"
targetHostCmd "nix-collect-garbage" "-d"
# Deliberately not quoting $deleteOlderThan so the user can configure something like "1 2 3"
# to keep generations with those numbers
targetHostCmd "nix-env" "--profile" "$profile" "--delete-generations" $deleteOlderThan
targetHostCmd "nix-store" "--gc"