nix.scm 6.2 KB

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