| 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 | (define-module (gnu services nix)
|
| 27 | #:use-module (gnu packages admin)
|
| 28 | #:use-module (gnu packages bash)
|
| 29 | #:use-module (gnu packages package-management)
|
| 30 | #:use-module (gnu services base)
|
| 31 | #:use-module (gnu services configuration)
|
| 32 | #:use-module (gnu services shepherd)
|
| 33 | #:use-module (gnu services web)
|
| 34 | #:use-module (gnu services)
|
| 35 | #:use-module (gnu system file-systems)
|
| 36 | #:use-module (gnu system shadow)
|
| 37 | #:use-module (guix gexp)
|
| 38 | #:use-module (guix packages)
|
| 39 | #:use-module (guix records)
|
| 40 | #:use-module (guix store)
|
| 41 | #:use-module (srfi srfi-1)
|
| 42 | #:use-module (srfi srfi-26)
|
| 43 | #:use-module (ice-9 match)
|
| 44 | #:use-module (ice-9 format)
|
| 45 | #:use-module (guix modules)
|
| 46 | #:export (nix-service-type
|
| 47 |
|
| 48 | nix-configuration
|
| 49 | nix-configuration?))
|
| 50 |
|
| 51 | (define-record-type* <nix-configuration>
|
| 52 | nix-configuration make-nix-configuration
|
| 53 | nix-configuration?
|
| 54 | (package nix-configuration-package |
| 55 | (default nix))
|
| 56 | (sandbox nix-configuration-sandbox |
| 57 | (default #t))
|
| 58 | (build-directory nix-configuration-build-directory |
| 59 | (default "/tmp"))
|
| 60 | (build-sandbox-items nix-configuration-build-sandbox-items |
| 61 | (default '()))
|
| 62 | (extra-config nix-configuration-extra-config |
| 63 | (default '()))
|
| 64 | (extra-options nix-configuration-extra-options |
| 65 | (default '())))
|
| 66 |
|
| 67 | |
| 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 | |
| 92 | |
| 93 | (id 40000))
|
| 94 | (nix-build-accounts 10 #:group "nixbld")))
|
| 95 |
|
| 96 | (define (nix-activation _)
|
| 97 | |
| 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 | |
| 129 | (format #t "build-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 | |
| 140 | |
| 141 | |
| 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 | |
| 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 | |