accounts.scm 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (gnu system accounts)
  19. #:use-module (guix records)
  20. #:use-module (ice-9 match)
  21. #:export (user-account
  22. user-account?
  23. user-account-name
  24. user-account-password
  25. user-account-uid
  26. user-account-group
  27. user-account-supplementary-groups
  28. user-account-comment
  29. user-account-home-directory
  30. user-account-create-home-directory?
  31. user-account-shell
  32. user-account-system?
  33. user-group
  34. user-group?
  35. user-group-name
  36. user-group-password
  37. user-group-id
  38. user-group-system?
  39. sexp->user-account
  40. sexp->user-group
  41. default-shell))
  42. ;;; Commentary:
  43. ;;;
  44. ;;; Data structures representing user accounts and user groups. This is meant
  45. ;;; to be used both on the host side and at run time--e.g., in activation
  46. ;;; snippets.
  47. ;;;
  48. ;;; Code:
  49. (define default-shell
  50. ;; Default shell for user accounts (a string or string-valued gexp).
  51. (make-parameter "/bin/sh"))
  52. (define-record-type* <user-account>
  53. user-account make-user-account
  54. user-account?
  55. (name user-account-name)
  56. (password user-account-password (default #f))
  57. (uid user-account-uid (default #f))
  58. (group user-account-group) ; number | string
  59. (supplementary-groups user-account-supplementary-groups
  60. (default '())) ; list of strings
  61. (comment user-account-comment (default ""))
  62. (home-directory user-account-home-directory (thunked)
  63. (default (default-home-directory this-record)))
  64. (create-home-directory? user-account-create-home-directory? ;Boolean
  65. (default #t))
  66. (shell user-account-shell ; gexp
  67. (default (default-shell)))
  68. (system? user-account-system? ; Boolean
  69. (default #f)))
  70. (define-record-type* <user-group>
  71. user-group make-user-group
  72. user-group?
  73. (name user-group-name)
  74. (password user-group-password (default #f))
  75. (id user-group-id (default #f))
  76. (system? user-group-system? ; Boolean
  77. (default #f)))
  78. (define (default-home-directory account)
  79. "Return the default home directory for ACCOUNT."
  80. (string-append "/home/" (user-account-name account)))
  81. (define (sexp->user-group sexp)
  82. "Take SEXP, a tuple as returned by 'user-group->gexp', and turn it into a
  83. user-group record."
  84. (match sexp
  85. ((name password id system?)
  86. (user-group (name name)
  87. (password password)
  88. (id id)
  89. (system? system?)))))
  90. (define (sexp->user-account sexp)
  91. "Take SEXP, a tuple as returned by 'user-account->gexp', and turn it into a
  92. user-account record."
  93. (match sexp
  94. ((name uid group supplementary-groups comment home-directory
  95. create-home-directory? shell password system?)
  96. (user-account (name name) (uid uid) (group group)
  97. (supplementary-groups supplementary-groups)
  98. (comment comment)
  99. (home-directory home-directory)
  100. (create-home-directory? create-home-directory?)
  101. (shell shell) (password password)
  102. (system? system?)))))