1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
;;; SPDX-License-Identifier: GPL-3.0-or-later
;;; SPDX-FileCopyrightText: 2026 Marek Paśnikowski <marek@marekpasnikowski.pl>
;;; COPYRIGHT NOTICE
;;;
;;; Copyright 2026, Marek Paśnikowski <marek@marekpasnikowski.pl>
;;; LICENSE NOTICE
;;;
;;; This library is free software: you can redistribute it and/or modify it under the terms of
;;; the GNU General Public License as published by the Free Software Foundation,
;;; either version 3 of the License, or (at your option) any later version.
;;;
;;; This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
;;; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
;;; See the GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License along with this library.
;;; If not, see <https://www.gnu.org/licenses/>.
(define-module (deployment services admin)
#:export (bash-aliases_service-record)
#:use-module (gnu home services)
#:use-module (gnu home services shells)
#:use-module (gnu services)
#:use-module (guix ui))
;; Retention threshold shared by the two generation scopes below: the
;; system generations and the machine-wide pruning of "guix gc -d".
;; Fail here, at build time, on anything "guix ui" does not parse as a
;; duration: with the argument absent, both commands fall back to
;; unrooting every generation but the current one, so an empty value
;; must never reach the command lines.
(define retention-duration_string "7d")
(unless (string->duration retention-duration_string)
(error "retention duration is not a duration string"
retention-duration_string))
;; As root, "guix gc -d" first unroots the profile and home-environment
;; generations of every user older than the retention duration, then
;; collects garbage in the same invocation.
(define collect-garbage_string
(string-append "sudo guix gc -d "
retention-duration_string))
;; Unroots the system generations older than the retention duration and
;; reinstalls the bootloader menu; the store space is reclaimed only by
;; the garbage collection pass.
(define system-delete-generations_string
(string-append "sudo guix system delete-generations "
retention-duration_string))
;; Updates the user's guix only. The system-wide guix is deployed by CI
;; through the channels mechanism, so this pull does not feed the
;; reconfigure below, and it stays unprivileged by design.
(define pull_string "guix pull --fallback")
;; Wrapped in "literal-string" in the alist below, so the serializer
;; single-quotes the whole value: the guard stays literal while .bashrc
;; is sourced and resolves at invocation, firing only when the host
;; name is empty, the kernel's own or one inherited from the environment.
(define system-reconfigure_string
"sudo guix system reconfigure -e \"(@ (deployment system) ${HOSTNAME:?HOSTNAME is not set})\" --fallback")
;; The order is causal: the generations unrooted above are reclaimed by
;; the collection pass of "guix gc -d", so it runs last, and "&&" stops
;; the chain before anything is unrooted or collected.
(define upgrade_string
(string-append pull_string
" && " system-reconfigure_string
" && " system-delete-generations_string
" && " collect-garbage_string))
(define bash-aliases_service-record
(simple-service 'bash-aliases
home-bash-service-type
(home-bash-extension
(aliases
`(("collect-garbage" . ,collect-garbage_string)
("delete-generations" . ,system-delete-generations_string)
("pull-guix" . ,pull_string)
("reconfigure-system" . ,(literal-string system-reconfigure_string))
("upgrade-system" . ,(literal-string upgrade_string)))))))
|