home.scm 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2021 Andrew Tropin <andrew@trop.in>
  3. ;;; Copyright © 2022 Ludovic Courtès <ludo@gnu.org>
  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 home)
  20. #:use-module (gnu home services)
  21. #:use-module (gnu home services symlink-manager)
  22. #:use-module (gnu home services shells)
  23. #:use-module (gnu home services xdg)
  24. #:use-module (gnu home services fontutils)
  25. #:use-module (gnu services)
  26. #:use-module (guix records)
  27. #:use-module (guix diagnostics)
  28. #:use-module (guix gexp)
  29. #:use-module (guix store)
  30. #:use-module (ice-9 match)
  31. #:use-module (ice-9 regex)
  32. #:export (home-environment
  33. home-environment?
  34. this-home-environment
  35. home-environment-derivation
  36. home-environment-packages
  37. home-environment-user-services
  38. home-environment-essential-services
  39. home-environment-services
  40. home-environment-location
  41. home-environment-with-provenance
  42. home-generation-base))
  43. ;;; Comment:
  44. ;;;
  45. ;;; This module provides a <home-environment> record for managing
  46. ;;; per-user packages and configuration files in the similar way as
  47. ;;; <operating-system> do for system packages and configuration files.
  48. ;;;
  49. ;;; Code:
  50. (define-record-type* <home-environment> home-environment
  51. make-home-environment
  52. home-environment?
  53. this-home-environment
  54. (packages home-environment-packages ; list of (PACKAGE OUTPUT...)
  55. (default '()))
  56. (essential-services home-environment-essential-services ; list of services
  57. (thunked)
  58. (default (home-environment-default-essential-services
  59. this-home-environment)))
  60. (services home-environment-user-services
  61. (default '()))
  62. (location home-environment-location ; <location>
  63. (default (and=> (current-source-location)
  64. source-properties->location))
  65. (innate)))
  66. (define (home-environment-default-essential-services he)
  67. "Return the list of essential services for home environment."
  68. (list
  69. (service home-run-on-first-login-service-type)
  70. (service home-activation-service-type)
  71. (service home-environment-variables-service-type)
  72. (service home-symlink-manager-service-type)
  73. (service home-fontconfig-service-type)
  74. (service home-xdg-base-directories-service-type)
  75. (service home-shell-profile-service-type)
  76. (service home-service-type)
  77. (service home-profile-service-type (home-environment-packages he))))
  78. (define* (home-environment-services he)
  79. "Return all the services of home environment."
  80. (instantiate-missing-services
  81. (append (home-environment-user-services he)
  82. (home-environment-essential-services he))))
  83. (define* (home-environment-derivation he)
  84. "Return a derivation that builds home environment."
  85. (let* ((services (home-environment-services he))
  86. (home (fold-services services
  87. #:target-type home-service-type)))
  88. (service-value home)))
  89. (define (home-environment-configuration-file he)
  90. "Return the configuration file of HE, based on its 'location' field, or #f
  91. if it could not be determined."
  92. (let ((file (and=> (home-environment-location he)
  93. location-file)))
  94. (and file
  95. (or (and (string-prefix? "/" file) file)
  96. (search-path %load-path file)))))
  97. (define* (home-environment-with-provenance he
  98. #:optional
  99. (config-file
  100. (home-environment-configuration-file
  101. he)))
  102. "Return a variant of HE that stores its own provenance information,
  103. including CONFIG-FILE, if available. This is achieved by adding an instance
  104. of HOME-PROVENANCE-SERVICE-TYPE to its services."
  105. (home-environment
  106. (inherit he)
  107. (services (cons (service home-provenance-service-type config-file)
  108. (home-environment-user-services he)))))
  109. (define-gexp-compiler (home-environment-compiler (he <home-environment>)
  110. system target)
  111. ((store-lift
  112. (lambda (store)
  113. (run-with-store store (home-environment-derivation he)
  114. #:system system
  115. #:target target)))))
  116. (define %profile-generation-rx
  117. ;; Regexp that matches profile generation.
  118. (make-regexp "(.*)-([0-9]+)-link$"))
  119. (define (home-generation-base file)
  120. "If FILE is a Home generation GC root such as \"guix-home-42-link\",
  121. return its corresponding base---e.g., \"guix-home\". Otherwise return #f.
  122. This is similar to the 'generation-profile' procedure but applied to Home
  123. generations."
  124. (match (regexp-exec %profile-generation-rx file)
  125. (#f #f)
  126. (m (let ((profile (match:substring m 1)))
  127. ;; Distinguish from a "real" profile and from a system generation.
  128. (and (file-exists? (string-append profile "/on-first-login"))
  129. (file-exists? (string-append profile "/profile/manifest"))
  130. profile)))))