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