nix.scm 5.9 KB

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