Merge remote-tracking branch 'origin/master' into wip-ungrafting
[jackhill/guix/guix.git] / gnu / services / nix.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2019, 2020, 2021 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 (gnu 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 shadow)
30 #:use-module (guix gexp)
31 #:use-module (guix packages)
32 #:use-module (guix records)
33 #:use-module (guix store)
34 #:use-module (srfi srfi-1)
35 #:use-module (srfi srfi-26)
36 #:use-module (ice-9 match)
37 #:use-module (ice-9 format)
38 #:use-module (guix modules)
39 #:export (nix-service-type
40
41 nix-configuration
42 nix-configuration?))
43
44 ;;; Commentary:
45 ;;;
46 ;;; This module provides a service definition for the Nix daemon.
47 ;;;
48 ;;; Code:
49
50 (define-record-type* <nix-configuration>
51 nix-configuration make-nix-configuration
52 nix-configuration?
53 (package nix-configuration-package ;package
54 (default nix))
55 (sandbox nix-configuration-sandbox ;boolean
56 (default #t))
57 (build-sandbox-items nix-configuration-build-sandbox-items ;list of strings
58 (default '()))
59 (extra-config nix-configuration-extra-config ;list of strings
60 (default '()))
61 (extra-options nix-configuration-extra-options ;list of strings
62 (default '())))
63
64 ;; Copied from gnu/services/base.scm
65 (define* (nix-build-accounts count #:key
66 (group "nixbld")
67 (shadow shadow))
68 "Return a list of COUNT user accounts for Nix build users with the given
69 GID."
70 (unfold (cut > <> count)
71 (lambda (n)
72 (user-account
73 (name (format #f "nixbld~2,'0d" n))
74 (system? #t)
75 (group group)
76 (supplementary-groups (list group "kvm"))
77 (comment (format #f "Nix Build User ~2d" n))
78 (home-directory "/var/empty")
79 (shell (file-append shadow "/sbin/nologin"))))
80 1+
81 1))
82 (define (nix-accounts _)
83 "Return the user accounts and user groups."
84 (cons (user-group
85 (name "nixbld")
86 (system? #t)
87
88 ;; Use a fixed GID so that we can create the store with the right
89 ;; owner.
90 (id 40000))
91 (nix-build-accounts 10 #:group "nixbld")))
92
93 (define (nix-activation _)
94 ;; Return the activation gexp.
95 #~(begin
96 (use-modules (guix build utils)
97 (srfi srfi-26))
98 (for-each (cut mkdir-p <>) '("/nix/store" "/nix/var/log"
99 "/nix/var/nix/gcroots/per-user"
100 "/nix/var/nix/profiles/per-user"))
101 (chown "/nix/store"
102 (passwd:uid (getpw "root")) (group:gid (getpw "nixbld01")))
103 (chmod "/nix/store" #o775)
104 (for-each (cut chmod <> #o777) '("/nix/var/nix/profiles"
105 "/nix/var/nix/profiles/per-user"))))
106
107 (define nix-service-etc
108 (match-lambda
109 (($ <nix-configuration> package sandbox build-sandbox-items extra-config)
110 (let ((ref-file (references-file package)))
111 `(("nix/nix.conf"
112 ,(computed-file
113 "nix.conf"
114 #~(begin
115 (use-modules (srfi srfi-26)
116 (ice-9 format))
117 (with-output-to-file #$output
118 (lambda _
119 (define internal-sandbox-paths
120 (call-with-input-file #$ref-file read))
121
122 (format #t "sandbox = ~a~%" (if #$sandbox "true" "false"))
123 ;; config.nix captures store file names.
124 (format #t "build-sandbox-paths = ~{~a ~}~%"
125 (append (list (string-append "/bin/sh=" #$bash-minimal "/bin/sh"))
126 internal-sandbox-paths
127 '#$build-sandbox-items))
128 (for-each (cut display <>) '#$extra-config)))))))))))
129
130 (define nix-shepherd-service
131 ;; Return a <shepherd-service> for Nix.
132 (match-lambda
133 (($ <nix-configuration> package _ _ _ extra-options)
134 (list
135 (shepherd-service
136 (provision '(nix-daemon))
137 (documentation "Run nix-daemon.")
138 (requirement '())
139 (start #~(make-forkexec-constructor
140 (list (string-append #$package "/bin/nix-daemon")
141 #$@extra-options)))
142 (respawn? #f)
143 (stop #~(make-kill-destructor)))))))
144
145 (define nix-service-type
146 (service-type
147 (name 'nix)
148 (extensions
149 (list (service-extension shepherd-root-service-type nix-shepherd-service)
150 (service-extension account-service-type nix-accounts)
151 (service-extension activation-service-type nix-activation)
152 (service-extension etc-service-type nix-service-etc)
153 (service-extension profile-service-type
154 (compose list nix-configuration-package))))
155 (description "Run the Nix daemon.")
156 (default-value (nix-configuration))))
157
158 ;;; nix.scm ends here