diff options
Diffstat (limited to 'modules/ryan-services')
-rw-r--r-- | modules/ryan-services/nix.scm | 182 |
1 files changed, 182 insertions, 0 deletions
diff --git a/modules/ryan-services/nix.scm b/modules/ryan-services/nix.scm new file mode 100644 index 0000000..75c9082 --- /dev/null +++ b/modules/ryan-services/nix.scm | |||
@@ -0,0 +1,182 @@ | |||
1 | ;;; GNU Guix --- Functional package management for GNU | ||
2 | ;;; Copyright © 2019, 2020, 2021, 2024 Oleg Pykhalov <go.wigust@gmail.com> | ||
3 | ;;; Copyright © 2020 Peng Mei Yu <i@pengmeiyu.com> | ||
4 | ;;; | ||
5 | ;;; This file is part of GNU Guix. | ||
6 | ;;; | ||
7 | ;;; GNU Guix is free software; you can redistribute it and/or modify it | ||
8 | ;;; under the terms of the GNU General Public License as published by | ||
9 | ;;; the Free Software Foundation; either version 3 of the License, or (at | ||
10 | ;;; your option) any later version. | ||
11 | ;;; | ||
12 | ;;; GNU Guix is distributed in the hope that it will be useful, but | ||
13 | ;;; WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
15 | ;;; GNU General Public License for more details. | ||
16 | ;;; | ||
17 | ;;; You should have received a copy of the GNU General Public License | ||
18 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. | ||
19 | |||
20 | (define-module (ryan-services nix) | ||
21 | #:use-module (gnu packages admin) | ||
22 | #:use-module (gnu packages bash) | ||
23 | #:use-module (gnu packages package-management) | ||
24 | #:use-module (gnu services base) | ||
25 | #:use-module (gnu services configuration) | ||
26 | #:use-module (gnu services shepherd) | ||
27 | #:use-module (gnu services web) | ||
28 | #:use-module (gnu services) | ||
29 | #:use-module (gnu system file-systems) | ||
30 | #:use-module (gnu system shadow) | ||
31 | #:use-module (guix gexp) | ||
32 | #:use-module (guix packages) | ||
33 | #:use-module (guix records) | ||
34 | #:use-module (guix store) | ||
35 | #:use-module (srfi srfi-1) | ||
36 | #:use-module (srfi srfi-26) | ||
37 | #:use-module (ice-9 match) | ||
38 | #:use-module (ice-9 format) | ||
39 | #:use-module (guix modules) | ||
40 | #:export (nix-service-type | ||
41 | |||
42 | nix-configuration | ||
43 | nix-configuration?)) | ||
44 | |||
45 | ;;; Commentary: | ||
46 | ;;; | ||
47 | ;;; This module provides a service definition for the Nix daemon. | ||
48 | ;;; | ||
49 | ;;; Code: | ||
50 | |||
51 | (define-record-type* <nix-configuration> | ||
52 | nix-configuration make-nix-configuration | ||
53 | nix-configuration? | ||
54 | (package nix-configuration-package ;file-like | ||
55 | (default nix)) | ||
56 | (sandbox nix-configuration-sandbox ;boolean | ||
57 | (default #t)) | ||
58 | (build-directory nix-configuration-build-directory ;string | ||
59 | (default "/tmp")) | ||
60 | (build-sandbox-items nix-configuration-build-sandbox-items ;list of strings | ||
61 | (default '())) | ||
62 | (extra-config nix-configuration-extra-config ;list of strings | ||
63 | (default '())) | ||
64 | (extra-options nix-configuration-extra-options ;list of strings | ||
65 | (default '()))) | ||
66 | |||
67 | ;; Copied from gnu/services/base.scm | ||
68 | (define* (nix-build-accounts count #:key | ||
69 | (group "nixbld") | ||
70 | (shadow shadow)) | ||
71 | "Return a list of COUNT user accounts for Nix build users with the given | ||
72 | GID." | ||
73 | (unfold (cut > <> count) | ||
74 | (lambda (n) | ||
75 | (user-account | ||
76 | (name (format #f "nixbld~2,'0d" n)) | ||
77 | (system? #t) | ||
78 | (group group) | ||
79 | (supplementary-groups (list group "kvm")) | ||
80 | (comment (format #f "Nix Build User ~2d" n)) | ||
81 | (home-directory "/var/empty") | ||
82 | (shell (file-append shadow "/sbin/nologin")))) | ||
83 | 1+ | ||
84 | 1)) | ||
85 | (define (nix-accounts _) | ||
86 | "Return the user accounts and user groups." | ||
87 | (cons (user-group | ||
88 | (name "nixbld") | ||
89 | (system? #t) | ||
90 | |||
91 | ;; Use a fixed GID so that we can create the store with the right | ||
92 | ;; owner. | ||
93 | (id 40000)) | ||
94 | (nix-build-accounts 10 #:group "nixbld"))) | ||
95 | |||
96 | (define (nix-activation _) | ||
97 | ;; Return the activation gexp. | ||
98 | #~(begin | ||
99 | (use-modules (guix build utils) | ||
100 | (srfi srfi-26)) | ||
101 | (for-each (cut mkdir-p <>) '("/nix/var/log" | ||
102 | "/nix/var/nix/gcroots/per-user" | ||
103 | "/nix/var/nix/profiles/per-user")) | ||
104 | (unless (file-exists? #$%nix-store-directory) | ||
105 | (mkdir-p #$%nix-store-directory) | ||
106 | (chown #$%nix-store-directory | ||
107 | (passwd:uid (getpw "root")) (group:gid (getpw "nixbld01"))) | ||
108 | (chmod #$%nix-store-directory #o775)) | ||
109 | (for-each (cut chmod <> #o777) '("/nix/var/nix/profiles" | ||
110 | "/nix/var/nix/profiles/per-user")))) | ||
111 | |||
112 | (define nix-service-etc | ||
113 | (match-lambda | ||
114 | (($ <nix-configuration> package sandbox build-directory build-sandbox-items extra-config) | ||
115 | (let ((ref-file (references-file package))) | ||
116 | `(("nix/nix.conf" | ||
117 | ,(computed-file | ||
118 | "nix.conf" | ||
119 | #~(begin | ||
120 | (use-modules (srfi srfi-26) | ||
121 | (ice-9 format)) | ||
122 | (with-output-to-file #$output | ||
123 | (lambda _ | ||
124 | (define internal-sandbox-paths | ||
125 | (call-with-input-file #$ref-file read)) | ||
126 | |||
127 | (format #t "sandbox = ~a~%" (if #$sandbox "true" "false")) | ||
128 | ;; config.nix captures store file names. | ||
129 | (format #t "sandbox-paths = ~{~a ~}~%" | ||
130 | (append (list (string-append "/bin/sh=" #$bash-minimal "/bin/bash")) | ||
131 | internal-sandbox-paths | ||
132 | '#$build-sandbox-items)) | ||
133 | (for-each (cut display <>) '#$extra-config))))))))))) | ||
134 | |||
135 | (define %nix-store-directory | ||
136 | "/nix/store") | ||
137 | |||
138 | (define %immutable-nix-store | ||
139 | ;; Read-only store to avoid users or daemons accidentally modifying it. | ||
140 | ;; 'nix-daemon' has provisions to remount it read-write in its own name | ||
141 | ;; space. | ||
142 | (list (file-system | ||
143 | (device %nix-store-directory) | ||
144 | (mount-point %nix-store-directory) | ||
145 | (type "none") | ||
146 | (check? #f) | ||
147 | (flags '(read-only bind-mount))))) | ||
148 | |||
149 | (define nix-shepherd-service | ||
150 | ;; Return a <shepherd-service> for Nix. | ||
151 | (match-lambda | ||
152 | (($ <nix-configuration> package _ build-directory _ _ extra-options) | ||
153 | (list | ||
154 | (shepherd-service | ||
155 | (provision '(nix-daemon)) | ||
156 | (documentation "Run nix-daemon.") | ||
157 | (requirement '(user-processes file-system-/nix/store)) | ||
158 | (start #~(make-forkexec-constructor | ||
159 | (list (string-append #$package "/bin/nix-daemon") | ||
160 | #$@extra-options) | ||
161 | #:environment-variables | ||
162 | (list (string-append "TMPDIR=" #$build-directory) | ||
163 | "PATH=/run/current-system/profile/bin"))) | ||
164 | (respawn? #f) | ||
165 | (stop #~(make-kill-destructor))))))) | ||
166 | |||
167 | (define nix-service-type | ||
168 | (service-type | ||
169 | (name 'nix) | ||
170 | (extensions | ||
171 | (list (service-extension shepherd-root-service-type nix-shepherd-service) | ||
172 | (service-extension account-service-type nix-accounts) | ||
173 | (service-extension activation-service-type nix-activation) | ||
174 | (service-extension etc-service-type nix-service-etc) | ||
175 | (service-extension profile-service-type | ||
176 | (compose list nix-configuration-package)) | ||
177 | (service-extension file-system-service-type | ||
178 | (const %immutable-nix-store)))) | ||
179 | (description "Run the Nix daemon.") | ||
180 | (default-value (nix-configuration)))) | ||
181 | |||
182 | ;;; nix.scm ends here | ||