services: Add missing (ice-9 format) import.
[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 #:use-module (ice-9 format)
35 #:export (nix-service-type))
36
37 ;;; Commentary:
38 ;;;
39 ;;; This module provides a service definition for the Nix daemon.
40 ;;;
41 ;;; Code:
42
43 \f
44 ;;;
45 ;;; Accounts
46 ;;;
47
48 ;; Copied from gnu/services/base.scm
49 (define* (nix-build-accounts count #:key
50 (group "nixbld")
51 (shadow shadow))
52 "Return a list of COUNT user accounts for Nix build users with the given
53 GID."
54 (unfold (cut > <> count)
55 (lambda (n)
56 (user-account
57 (name (format #f "nixbld~2,'0d" n))
58 (system? #t)
59 (group group)
60 (supplementary-groups (list group "kvm"))
61 (comment (format #f "Nix Build User ~2d" n))
62 (home-directory "/var/empty")
63 (shell (file-append shadow "/sbin/nologin"))))
64 1+
65 1))
66 (define (nix-accounts _)
67 "Return the user accounts and user groups."
68 (cons (user-group
69 (name "nixbld")
70 (system? #t)
71
72 ;; Use a fixed GID so that we can create the store with the right
73 ;; owner.
74 (id 40000))
75 (nix-build-accounts 10 #:group "nixbld")))
76
77 (define (nix-activation _)
78 "Return the activation gexp."
79 (with-imported-modules '((guix build utils))
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