nix.scm 5.8 KB

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