services: Add SRFI-26 to Nix activation gexp.
[jackhill/guix/guix.git] / gnu / services / nix.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2019 Oleg Pykhalov <go.wigust@gmail.com>
3 ;;;
4 ;;; This file is part of GNU Guix.
5 ;;;
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
10 ;;;
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
15 ;;;
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19 (define-module (gnu services nix)
20 #:use-module (gnu packages admin)
21 #:use-module (gnu packages package-management)
22 #:use-module (gnu services base)
23 #:use-module (gnu services configuration)
24 #:use-module (gnu services shepherd)
25 #:use-module (gnu services web)
26 #:use-module (gnu services)
27 #:use-module (gnu system shadow)
28 #:use-module (guix gexp)
29 #:use-module (guix packages)
30 #:use-module (guix records)
31 #:use-module (guix store)
32 #:use-module (srfi srfi-1)
33 #:use-module (srfi srfi-26)
34 #:export (nix-service-type))
35
36 ;;; Commentary:
37 ;;;
38 ;;; This module provides a service definition for the Nix daemon.
39 ;;;
40 ;;; Code:
41
42 \f
43 ;;;
44 ;;; Accounts
45 ;;;
46
47 ;; Copied from gnu/services/base.scm
48 (define* (nix-build-accounts count #:key
49 (group "nixbld")
50 (shadow shadow))
51 "Return a list of COUNT user accounts for Nix build users with the given
52 GID."
53 (unfold (cut > <> count)
54 (lambda (n)
55 (user-account
56 (name (format #f "nixbld~2,'0d" n))
57 (system? #t)
58 (group group)
59 (supplementary-groups (list group "kvm"))
60 (comment (format #f "Nix Build User ~2d" n))
61 (home-directory "/var/empty")
62 (shell (file-append shadow "/sbin/nologin"))))
63 1+
64 1))
65 (define (nix-accounts _)
66 "Return the user accounts and user groups."
67 (cons (user-group
68 (name "nixbld")
69 (system? #t)
70
71 ;; Use a fixed GID so that we can create the store with the right
72 ;; owner.
73 (id 40000))
74 (nix-build-accounts 10 #:group "nixbld")))
75
76 (define (nix-activation _)
77 "Return the activation gexp."
78 (with-imported-modules '((guix build utils)
79 (srfi srfi-26))
80 #~(begin
81 (use-modules (guix build utils)
82 (srfi srfi-26))
83 (for-each (cut mkdir-p <>) '("/nix/store" "/nix/var/log"
84 "/nix/var/nix/gcroots/per-user"
85 "/nix/var/nix/profiles/per-user"))
86 (chown "/nix/store"
87 (passwd:uid (getpw "root")) (group:gid (getpw "nixbld01")))
88 (chmod "/nix/store" #o775)
89 (for-each (cut chmod <> #o777) '("/nix/var/nix/profiles"
90 "/nix/var/nix/profiles/per-user")))))
91
92 (define (nix-shepherd-service _)
93 "Return a <shepherd-service> for Nix."
94 (list
95 (shepherd-service
96 (provision '(nix-daemon))
97 (documentation "Run nix-daemon.")
98 (requirement '())
99 (start #~(make-forkexec-constructor
100 (list (string-append #$nix "/bin/nix-daemon"))))
101 (respawn? #f)
102 (stop #~(make-kill-destructor)))))
103
104 (define nix-service-type
105 (service-type
106 (name 'nix)
107 (extensions
108 (list (service-extension shepherd-root-service-type nix-shepherd-service)
109 (service-extension account-service-type nix-accounts)
110 (service-extension activation-service-type nix-activation)))
111 (default-value '())
112 (description "Run the Nix daemon.")))
113
114 ;;; nix.scm ends here