Skip to content

Commit 73519ae

Browse files
authored
Add option to keep old generations (#55)
1 parent c7459ec commit 73519ae

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

deploy_nixos/main.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ variable "hermetic" {
9999
default = false
100100
}
101101

102+
variable "delete_older_than" {
103+
type = string
104+
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."
105+
default = "+1"
106+
}
107+
102108
# --------------------------------------------------------------------------
103109

104110
locals {
@@ -184,6 +190,7 @@ resource "null_resource" "deploy_nixos" {
184190
local.build_on_target,
185191
local.ssh_private_key == "" ? "-" : local.ssh_private_key,
186192
"switch",
193+
var.delete_older_than,
187194
],
188195
local.extra_build_args
189196
)

deploy_nixos/nixos-deploy.sh

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ targetPort="$4"
3333
buildOnTarget="$5"
3434
sshPrivateKey="$6"
3535
action="$7"
36-
shift 7
36+
deleteOlderThan="$8"
37+
shift 8
3738

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

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

0 commit comments

Comments
 (0)