2019-05-09 12:13:26 +00:00
|
|
|
;;; GNU Guix --- Functional package management for GNU
|
2021-04-22 16:46:23 +00:00
|
|
|
;;; Copyright © 2019, 2020, 2021 Oleg Pykhalov <go.wigust@gmail.com>
|
2020-07-23 09:24:13 +00:00
|
|
|
;;; Copyright © 2020 Peng Mei Yu <i@pengmeiyu.com>
|
2019-05-09 12:13:26 +00:00
|
|
|
;;;
|
|
|
|
;;; This file is part of GNU Guix.
|
|
|
|
;;;
|
|
|
|
;;; GNU Guix is free software; you can redistribute it and/or modify it
|
|
|
|
;;; under the terms of the GNU General Public License as published by
|
|
|
|
;;; the Free Software Foundation; either version 3 of the License, or (at
|
|
|
|
;;; your option) any later version.
|
|
|
|
;;;
|
|
|
|
;;; GNU Guix is distributed in the hope that it will be useful, but
|
|
|
|
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;;; GNU General Public License for more details.
|
|
|
|
;;;
|
|
|
|
;;; You should have received a copy of the GNU General Public License
|
|
|
|
;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
(define-module (gnu services nix)
|
|
|
|
#:use-module (gnu packages admin)
|
2021-04-22 16:46:23 +00:00
|
|
|
#:use-module (gnu packages bash)
|
2019-05-09 12:13:26 +00:00
|
|
|
#:use-module (gnu packages package-management)
|
|
|
|
#:use-module (gnu services base)
|
|
|
|
#:use-module (gnu services configuration)
|
|
|
|
#:use-module (gnu services shepherd)
|
|
|
|
#:use-module (gnu services web)
|
|
|
|
#:use-module (gnu services)
|
|
|
|
#:use-module (gnu system shadow)
|
|
|
|
#:use-module (guix gexp)
|
|
|
|
#:use-module (guix packages)
|
|
|
|
#:use-module (guix records)
|
|
|
|
#:use-module (guix store)
|
|
|
|
#:use-module (srfi srfi-1)
|
|
|
|
#:use-module (srfi srfi-26)
|
2020-07-22 06:47:16 +00:00
|
|
|
#:use-module (ice-9 match)
|
2020-06-24 08:47:23 +00:00
|
|
|
#:use-module (ice-9 format)
|
2020-07-22 06:47:16 +00:00
|
|
|
#:use-module (guix modules)
|
2020-07-24 06:11:43 +00:00
|
|
|
#:export (nix-service-type
|
|
|
|
|
|
|
|
nix-configuration
|
|
|
|
nix-configuration?))
|
2019-05-09 12:13:26 +00:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
;;;
|
|
|
|
;;; This module provides a service definition for the Nix daemon.
|
|
|
|
;;;
|
|
|
|
;;; Code:
|
|
|
|
|
2020-07-22 06:47:16 +00:00
|
|
|
(define-record-type* <nix-configuration>
|
|
|
|
nix-configuration make-nix-configuration
|
|
|
|
nix-configuration?
|
2021-11-18 21:44:26 +00:00
|
|
|
(package nix-configuration-package ;file-like
|
2020-07-22 06:47:16 +00:00
|
|
|
(default nix))
|
|
|
|
(sandbox nix-configuration-sandbox ;boolean
|
|
|
|
(default #t))
|
2022-10-31 15:12:38 +00:00
|
|
|
(build-directory nix-configuration-build-directory ;string
|
|
|
|
(default "/tmp"))
|
2020-07-22 06:47:16 +00:00
|
|
|
(build-sandbox-items nix-configuration-build-sandbox-items ;list of strings
|
|
|
|
(default '()))
|
2020-07-25 08:48:37 +00:00
|
|
|
(extra-config nix-configuration-extra-config ;list of strings
|
2020-07-25 08:51:49 +00:00
|
|
|
(default '()))
|
|
|
|
(extra-options nix-configuration-extra-options ;list of strings
|
2020-07-22 06:47:16 +00:00
|
|
|
(default '())))
|
2019-05-09 12:13:26 +00:00
|
|
|
|
|
|
|
;; Copied from gnu/services/base.scm
|
|
|
|
(define* (nix-build-accounts count #:key
|
|
|
|
(group "nixbld")
|
|
|
|
(shadow shadow))
|
|
|
|
"Return a list of COUNT user accounts for Nix build users with the given
|
|
|
|
GID."
|
|
|
|
(unfold (cut > <> count)
|
|
|
|
(lambda (n)
|
|
|
|
(user-account
|
|
|
|
(name (format #f "nixbld~2,'0d" n))
|
|
|
|
(system? #t)
|
|
|
|
(group group)
|
|
|
|
(supplementary-groups (list group "kvm"))
|
|
|
|
(comment (format #f "Nix Build User ~2d" n))
|
|
|
|
(home-directory "/var/empty")
|
|
|
|
(shell (file-append shadow "/sbin/nologin"))))
|
|
|
|
1+
|
|
|
|
1))
|
|
|
|
(define (nix-accounts _)
|
|
|
|
"Return the user accounts and user groups."
|
|
|
|
(cons (user-group
|
|
|
|
(name "nixbld")
|
|
|
|
(system? #t)
|
|
|
|
|
|
|
|
;; Use a fixed GID so that we can create the store with the right
|
|
|
|
;; owner.
|
|
|
|
(id 40000))
|
|
|
|
(nix-build-accounts 10 #:group "nixbld")))
|
|
|
|
|
2020-12-06 08:26:02 +00:00
|
|
|
(define (nix-activation _)
|
2020-07-22 06:47:16 +00:00
|
|
|
;; Return the activation gexp.
|
2020-12-06 08:26:02 +00:00
|
|
|
#~(begin
|
|
|
|
(use-modules (guix build utils)
|
|
|
|
(srfi srfi-26))
|
|
|
|
(for-each (cut mkdir-p <>) '("/nix/store" "/nix/var/log"
|
|
|
|
"/nix/var/nix/gcroots/per-user"
|
|
|
|
"/nix/var/nix/profiles/per-user"))
|
|
|
|
(chown "/nix/store"
|
|
|
|
(passwd:uid (getpw "root")) (group:gid (getpw "nixbld01")))
|
|
|
|
(chmod "/nix/store" #o775)
|
|
|
|
(for-each (cut chmod <> #o777) '("/nix/var/nix/profiles"
|
|
|
|
"/nix/var/nix/profiles/per-user"))))
|
|
|
|
|
|
|
|
(define nix-service-etc
|
2020-07-22 06:47:16 +00:00
|
|
|
(match-lambda
|
2022-10-31 15:12:38 +00:00
|
|
|
(($ <nix-configuration> package sandbox build-directory build-sandbox-items extra-config)
|
2020-12-06 08:26:02 +00:00
|
|
|
(let ((ref-file (references-file package)))
|
|
|
|
`(("nix/nix.conf"
|
|
|
|
,(computed-file
|
|
|
|
"nix.conf"
|
|
|
|
#~(begin
|
|
|
|
(use-modules (srfi srfi-26)
|
|
|
|
(ice-9 format))
|
|
|
|
(with-output-to-file #$output
|
|
|
|
(lambda _
|
|
|
|
(define internal-sandbox-paths
|
|
|
|
(call-with-input-file #$ref-file read))
|
|
|
|
|
|
|
|
(format #t "sandbox = ~a~%" (if #$sandbox "true" "false"))
|
|
|
|
;; config.nix captures store file names.
|
|
|
|
(format #t "build-sandbox-paths = ~{~a ~}~%"
|
2021-04-22 16:46:23 +00:00
|
|
|
(append (list (string-append "/bin/sh=" #$bash-minimal "/bin/sh"))
|
|
|
|
internal-sandbox-paths
|
2020-12-06 08:26:02 +00:00
|
|
|
'#$build-sandbox-items))
|
|
|
|
(for-each (cut display <>) '#$extra-config)))))))))))
|
2019-05-09 12:13:26 +00:00
|
|
|
|
2020-07-22 06:47:16 +00:00
|
|
|
(define nix-shepherd-service
|
|
|
|
;; Return a <shepherd-service> for Nix.
|
|
|
|
(match-lambda
|
2022-10-31 15:12:38 +00:00
|
|
|
(($ <nix-configuration> package _ build-directory _ _ extra-options)
|
2020-07-22 06:47:16 +00:00
|
|
|
(list
|
|
|
|
(shepherd-service
|
|
|
|
(provision '(nix-daemon))
|
|
|
|
(documentation "Run nix-daemon.")
|
|
|
|
(requirement '())
|
|
|
|
(start #~(make-forkexec-constructor
|
2020-07-25 08:51:49 +00:00
|
|
|
(list (string-append #$package "/bin/nix-daemon")
|
2022-10-31 15:12:38 +00:00
|
|
|
#$@extra-options)
|
|
|
|
#:environment-variables
|
2022-12-15 10:13:24 +00:00
|
|
|
(list (string-append "TMPDIR=" #$build-directory)
|
|
|
|
"PATH=/run/current-system/profile/bin")))
|
2020-07-22 06:47:16 +00:00
|
|
|
(respawn? #f)
|
|
|
|
(stop #~(make-kill-destructor)))))))
|
2019-05-09 12:13:26 +00:00
|
|
|
|
|
|
|
(define nix-service-type
|
|
|
|
(service-type
|
|
|
|
(name 'nix)
|
|
|
|
(extensions
|
|
|
|
(list (service-extension shepherd-root-service-type nix-shepherd-service)
|
|
|
|
(service-extension account-service-type nix-accounts)
|
2020-07-23 09:24:13 +00:00
|
|
|
(service-extension activation-service-type nix-activation)
|
2020-12-06 08:26:02 +00:00
|
|
|
(service-extension etc-service-type nix-service-etc)
|
2020-07-23 09:24:13 +00:00
|
|
|
(service-extension profile-service-type
|
|
|
|
(compose list nix-configuration-package))))
|
2020-07-22 06:47:16 +00:00
|
|
|
(description "Run the Nix daemon.")
|
|
|
|
(default-value (nix-configuration))))
|
2019-05-09 12:13:26 +00:00
|
|
|
|
|
|
|
;;; nix.scm ends here
|