;;; GNU Guix --- Functional package management for GNU ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 Ludovic Courtès ;;; Copyright © 2017 Efraim Flashner ;;; Copyright © 2018 Tobias Geerinckx-Rice ;;; Copyright © 2018, 2019 Mark H Weaver ;;; Copyright © 2018, 2019 Jan (janneke) Nieuwenhuizen ;;; Copyright © 2019 Marius Bakke ;;; ;;; This file is part of GNU Guix. ;;; ;;; GNU Guix is free software; you can redistribute it and/or modify it ;;; under the terms of the GNU General Public License as published by ;;; the Free Software Foundation; either version 3 of the License, or (at ;;; your option) any later version. ;;; ;;; GNU Guix is distributed in the hope that it will be useful, but ;;; WITHOUT ANY WARRANTY; without even the implied warranty of ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;;; GNU General Public License for more details. ;;; ;;; You shoul
aboutsummaryrefslogtreecommitdiff
blob: 673b96c71320d73685739558a33bbc7aaef5b84c (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2015, 2018 Ludovic Courtès <ludo@gnu.org>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (gnu system nss)
  #:use-module (rnrs enums)
  #:use-module (guix records)
  #:use-module (srfi srfi-9)
  #:use-module (ice-9 match)
  #:export (name-service-switch?
            name-service-switch
            name-service?
            name-service

            lookup-specification

            %default-nss
            %mdns-host-lookup-nss

            %files
            %compat
            %dns

            name-service-switch->string))

;;; Commentary:
;;;
;;; Bindings for libc's name service switch (NSS) configuration.
;;;
;;; Code:

(define-record-type* <name-service> name-service
  make-name-service
  name-service?
  (name     name-service-name)
  (reaction name-service-reaction
            (default (lookup-specification))))

;; Lookup specification (info "(libc) Actions in the NSS Configuration").

(define-enumeration lookup-action
  (return continue)
  make-lookup-action)

(define-enumeration lookup-status
  (success
   not-found
   unavailable
   try-again)
  make-lookup-status)

(define-record-type <lookup-status-negation>
  (lookup-status-negation status)
  lookup-status-negation?
  (status lookup-status-negation-status))

(define-record-type <lookup-reaction>
  (make-lookup-reaction status action)
  lookup-reaction?
  (status  lookup-reaction-status)
  (action  lookup-reaction-action))

(define-syntax lookup-reaction
  (syntax-rules (not =>)
    ((_ ((not status) => action))
     (make-lookup-reaction (lookup-status-negation (lookup-status status))
                           (lookup-action action)))
    ((_ (status => action))
     (make-lookup-reaction (lookup-status status)
                           (lookup-action action)))))

(define-syntax-rule (lookup-specification reaction ...)
  "Return an NSS lookup specification."
  (list (lookup-reaction reaction) ...))


;;;
;;; Common name services and default NSS configuration.
;;;

(define %compat
  ;; Note: Starting from version 2.26, libc no longer provides libnss_compat
  ;; so this specification has become useless.
  (name-service
    (name "compat")
    (reaction (lookup-specification (not-found => return)))))

(define %files
  (name-service (name "files")))

(define %dns
  ;; DNS is supposed to be authoritative, so unless it's unavailable, return
  ;; what it finds.
  (name-service
    (name "dns")
    (reaction (lookup-specification ((not unavailable) => return)))))

;; The NSS.  We list all the databases here because that allows us to
;; statically ensure that the user's configuration refers to existing
;; databases.  See libc/nss/databases.def for the list of databases.  Default
;; values obtained by looking for "DEFAULT_CONFIG" in libc/nss/*.c.
;;
;; Although libc places 'dns' before 'files' in the default configurations of
;; the 'hosts' and 'networks' databases, we choose to put 'files' before 'dns'
;; by default, so that users can override host/address mappings in /etc/hosts
;; and bypass DNS to improve their privacy and escape NSA's MORECOWBELL.
(define-record-type* <name-service-switch> name-service-switch
  make-name-service-switch
  name-service-switch?
  (aliases    name-service-switch-aliases
              (default '()))
  (ethers     name-service-switch-ethers
              (default '()))
  (group      name-service-switch-group
              (default (list %files)))
  (gshadow    name-service-switch-gshadow
              (default '()))
  (hosts      name-service-switch-hosts
              (default (list %files %dns)))
  (initgroups name-service-switch-initgroups
              (default '()))
  (netgroup   name-service-switch-netgroup
              (default '()))
  (networks   name-service-switch-networks
              (default (list %files %dns)))
  (password   name-service-switch-password
              (default (list %files)))
  (public-key name-service-switch-public-key
              (default '()))
  (rpc        name-service-switch-rpc
              (default '()))
  (services   name-service-switch-services
              (default '()))
  (shadow     name-service-switch-shadow
              (default (list %files))))

(define %default-nss
  ;; Default NSS configuration.
  (name-service-switch))

(define %mdns-host-lookup-nss
  (name-service-switch
    (hosts (list %files                           ;first, check /etc/hosts

                 ;; If the above did not succeed, try with 'mdns_minimal'.
                 (name-service
                   (name "mdns_minimal")

                   ;; 'mdns_minimal' is authoritative for '.local'.  When it
                   ;; returns "not found", no need to try the next methods.
                   (reaction (lookup-specification
                              (not-found => return))))

                 ;; Then fall back to DNS.
                 (name-service
                   (name "dns"))

                 ;; Finally, try with the "full" 'mdns'.
                 (name-service
                   (name "mdns"))))))


;;;
;;; Serialization.
;;;

(define (lookup-status->string status)
  (match status
    ('success     "SUCCESS")
    ('not-found   "NOTFOUND")
    ('unavailable "UNAVAIL")
    ('try-again   "TRYAGAIN")
    (($ <lookup-status-negation> status)
     (string-append "!" (lookup-status->string status)))))

(define lookup-reaction->string
  (match-lambda
   (($ <lookup-reaction> status action)
    (string-append (lookup-status->string status) "="
                   (symbol->string action)))))

(define name-service->string
  (match-lambda
   (($ <name-service> name ())
    name)
   (($ <name-service> name reactions)
    (string-append name " ["
                   (string-join (map lookup-reaction->string reactions))
                   "]"))))

(define (name-service-switch->string nss)
  "Return the 'nsswitch.conf' contents for NSS as a string.  See \"NSS
Configuration File\" in the libc manual."
  (let-syntax ((->string
                (syntax-rules ()
                  ((_ name field)
                   (match (field nss)
                     (()                          ;keep the default config
                      "")
                     ((services (... ...))
                      (string-append name ":\t"
                                     (string-join
                                      (map name-service->string services))
                                     "\n")))))))
    (string-append (->string "aliases"    name-service-switch-aliases)
                   (->string "ethers"     name-service-switch-ethers)
                   (->string "group"      name-service-switch-group)
                   (->string "gshadow"    name-service-switch-gshadow)
                   (->string "hosts"      name-service-switch-hosts)
                   (->string "initgroups" name-service-switch-initgroups)
                   (->string "netgroup"   name-service-switch-netgroup)
                   (->string "networks"   name-service-switch-networks)
                   (->string "passwd"     name-service-switch-password)
                   (->string "publickey"  name-service-switch-public-key)
                   (->string "rpc"        name-service-switch-rpc)
                   (->string "services"   name-service-switch-services)
                   (->string "shadow"     name-service-switch-shadow))))

;;; Local Variables:
;;; eval: (put 'name-service 'scheme-indent-function 0)
;;; eval: (put 'name-service-switch 'scheme-indent-function 0)
;;; End:

;;; nss.scm ends here
"ranlib" "readelf" "size" "strings" "strip")) #t)))) (inputs `(("binutils" ,%binutils-static))))) (define (%glibc-stripped) ;; GNU libc's essential shared libraries, dynamic linker, and headers, ;; with all references to store directories stripped. As a result, ;; libc.so is unusable and need to be patched for proper relocation. (let ((glibc (glibc-for-bootstrap glibc))) (package (inherit glibc) (name "glibc-stripped") (build-system trivial-build-system) (arguments `(#:modules ((guix build utils) (guix build make-bootstrap)) #:builder (begin (use-modules (guix build make-bootstrap)) (make-stripped-libc (assoc-ref %outputs "out") (assoc-ref %build-inputs "libc") (assoc-ref %build-inputs "kernel-headers"))))) (inputs `(("kernel-headers" ,(if (or (and (%current-target-system) (hurd-triplet? (%current-target-system))) (string-suffix? "-hurd" (%current-system))) gnumach-headers linux-libre-headers)) ("libc" ,(let ((target (%current-target-system))) (if target (glibc-for-bootstrap (parameterize ((%current-target-system #f)) (cross-libc target))) glibc))))) (native-inputs '()) (propagated-inputs '()) ;; Only one output. (outputs '("out"))))) (define %gcc-static ;; A statically-linked GCC, with stripped-down functionality. (package-with-relocatable-glibc (package (inherit gcc) (name "gcc-static") (outputs '("out")) ; all in one (arguments `(#:modules ((guix build utils) (guix build gnu-build-system) (srfi srfi-1) (srfi srfi-26) (ice-9 regex)) ,@(substitute-keyword-arguments (package-arguments gcc) ((#:guile _) #f) ((#:implicit-inputs? _) #t) ((#:configure-flags flags) `(append (list ;; We don't need a full bootstrap here. "--disable-bootstrap" ;; Make sure '-static' is passed where it matters. "--with-stage1-ldflags=-static" ;; GCC 4.8+ requires a C++ compiler and library. "--enable-languages=c,c++" ;; Make sure gcc-nm doesn't require liblto_plugin.so. "--disable-lto" "--disable-shared" "--disable-plugin" "--disable-libmudflap" "--disable-libatomic" "--disable-libsanitizer" "--disable-libitm" "--disable-libgomp" "--disable-libcilkrts" "--disable-libvtv" "--disable-libssp" "--disable-libquadmath") (remove (cut string-match "--(.*plugin|enable-languages)" <>) ,flags))) ((#:phases phases) `(modify-phases ,phases (add-after 'pre-configure 'remove-lgcc_s (lambda _ ;; Remove the '-lgcc_s' added to GNU_USER_TARGET_LIB_SPEC in ;; the 'pre-configure phase of our main gcc package, because ;; that shared library is not present in this static gcc. See ;; . (substitute* (cons "gcc/config/rs6000/sysv4.h" (find-files "gcc/config" "^gnu-user.*\\.h$")) ((" -lgcc_s}}") "}}")) #t))))))) (inputs `(("zlib:static" ,zlib "static") ,@(package-inputs gcc))) (native-inputs (if (%current-target-system) `(;; When doing a Canadian cross, we need GMP/MPFR/MPC both ;; as target inputs and as native inputs; the latter is ;; needed when building build-time tools ('genconstants', ;; etc.) Failing to do that leads to misdetections of ;; declarations by 'gcc/configure', and eventually to ;; duplicate declarations as reported in ;; . ("gmp-native" ,gmp) ("mpfr-native" ,mpfr) ("mpc-native" ,mpc) ,@(package-native-inputs gcc)) (package-native-inputs gcc)))))) (define %gcc-stripped ;; The subset of GCC files needed for bootstrap. (package (inherit gcc) (name "gcc-stripped") (build-system trivial-build-system) (source #f) (outputs '("out")) ;only one output (arguments `(#:modules ((guix build utils)) #:builder (begin (use-modules (srfi srfi-1) (srfi srfi-26) (guix build utils)) (setvbuf (current-output-port) _IOLBF) (let* ((out (assoc-ref %outputs "out")) (bindir (string-append out "/bin")) (libdir (string-append out "/lib")) (includedir (string-append out "/include")) (libexecdir (string-append out "/libexec")) (gcc (assoc-ref %build-inputs "gcc"))) (copy-recursively (string-append gcc "/bin") bindir) (for-each remove-store-references (find-files bindir ".*")) (copy-recursively (string-append gcc "/lib") libdir) (for-each remove-store-references (remove (cut string-suffix? ".h" <>) (find-files libdir ".*"))) (copy-recursively (string-append gcc "/libexec") libexecdir) (for-each remove-store-references (find-files libexecdir ".*")) ;; Starting from GCC 4.8, helper programs built natively ;; (‘genchecksum’, ‘gcc-nm’, etc.) rely on C++ headers. (copy-recursively (string-append gcc "/include/c++") (string-append includedir "/c++")) ;; For native builds, check whether the binaries actually work. ,@(if (%current-target-system) '() '((for-each (lambda (prog) (invoke (string-append gcc "/bin/" prog) "--version")) '("gcc" "g++" "cpp")))) #t)))) (inputs `(("gcc" ,%gcc-static))))) ;; Two packages: first build static, bare minimum content. (define %mescc-tools-static ;; A statically linked MesCC Tools. (package (inherit mescc-tools-0.5.2) (name "mescc-tools-static") (arguments `(#:system "i686-linux" ,@(substitute-keyword-arguments (package-arguments mescc-tools) ((#:make-flags flags) `(cons "CC=gcc -static" ,flags))))))) ;; ... next remove store references. (define %mescc-tools-static-stripped ;; A statically linked Mescc Tools with store references removed, for ;; bootstrap. (package (inherit %mescc-tools-static) (name (string-append (package-name %mescc-tools-static) "-stripped")) (build-system trivial-build-system) (arguments `(#:modules ((guix build utils)) #:builder (begin (use-modules (guix build utils)) (let* ((in (assoc-ref %build-inputs "mescc-tools")) (out (assoc-ref %outputs "out")) (bin (string-append out "/bin"))) (mkdir-p bin) (for-each (lambda (file) (let ((target (string-append bin "/" file))) (format #t "copying `~a'...~%" file) (copy-file (string-append in "/bin/" file) target) (remove-store-references target))) '( "M1" "blood-elf" "hex2")) #t)))) (inputs `(("mescc-tools" ,%mescc-tools-static))))) ;; Two packages: first build static, bare minimum content. (define-public %mes-minimal ;; A minimal Mes without documentation. (let ((triplet "i686-unknown-linux-gnu")) (package (inherit mes-0.19) (name "mes-minimal") (native-inputs `(("guile" ,guile-2.2))) (arguments `(#:system "i686-linux" #:strip-binaries? #f #:configure-flags '("--mes") #:phases (modify-phases %standard-phases (delete 'patch-shebangs) (add-after 'install 'strip-install (lambda _ (let* ((out (assoc-ref %outputs "out")) (share (string-append out "/share"))) (delete-file-recursively (string-append out "/lib/guile")) (delete-file-recursively (string-append share "/guile")) (delete-file-recursively (string-append share "/mes/scaffold")) (for-each delete-file (find-files (string-append share "/mes/lib") "\\.(h|c)"))))))))))) ;; next remove store references. (define %mes-minimal-stripped ;; A minimal Mes with store references removed, for bootstrap. (package (inherit %mes-minimal) (name (string-append (package-name %mes-minimal) "-stripped")) (build-system trivial-build-system) (arguments `(#:modules ((guix build utils)) #:builder (begin (use-modules (guix build utils)) (let ((in (assoc-ref %build-inputs "mes")) (out (assoc-ref %outputs "out"))) (copy-recursively in out) (for-each (lambda (dir) (for-each remove-store-references (find-files (string-append out "/" dir) ".*"))) '("bin" "share/mes")) #t)))) (inputs `(("mes" ,%mes-minimal))))) (define %guile-static ;; A statically-linked Guile that is relocatable--i.e., it can search ;; .scm and .go files relative to its installation directory, rather ;; than in hard-coded configure-time paths. (let* ((patches (cons* (search-patch "guile-relocatable.patch") (search-patch "guile-2.2-default-utf8.patch") (search-patch "guile-linux-syscalls.patch") (origin-patches (package-source guile-2.2)))) (source (origin (inherit (package-source guile-2.2)) (patches patches))) (guile (package (inherit guile-2.2) (name (string-append (package-name guile-2.2) "-static")) (source source) (synopsis "Statically-linked and relocatable Guile") ;; Remove the 'debug' output (see above for the reason.) (outputs (delete "debug" (package-outputs guile-2.2))) (inputs `(("libunistring:static" ,libunistring "static") ,@(package-inputs guile-2.2))) (propagated-inputs `(("bdw-gc" ,libgc) ,@(alist-delete "bdw-gc" (package-propagated-inputs guile-2.2)))) (arguments (substitute-keyword-arguments (package-arguments guile-2.2) ((#:configure-flags flags '()) ;; When `configure' checks for ltdl availability, it ;; doesn't try to link using libtool, and thus fails ;; because of a missing -ldl. Work around that. ''("LDFLAGS=-ldl")) ((#:phases phases '%standard-phases) `(modify-phases ,phases ;; Do not record the absolute file name of 'sh' in ;; (ice-9 popen). This makes 'open-pipe' unusable in ;; a build chroot ('open-pipe*' is fine) but avoids ;; keeping a reference to Bash. (delete 'pre-configure) (add-before 'configure 'static-guile (lambda _ (substitute* "libguile/Makefile.in" ;; Create a statically-linked `guile' ;; executable. (("^guile_LDFLAGS =") "guile_LDFLAGS = -all-static") ;; Add `-ldl' *after* libguile-2.2.la. (("^guile_LDADD =(.*)$" _ ldadd) (string-append "guile_LDADD = " (string-trim-right ldadd) " -ldl\n"))))))) ((#:tests? _ #f) ;; There are uses of `dynamic-link' in ;; {foreign,coverage}.test that don't fly here. #f) ((#:parallel-build? _ #f) ;; Work around the fact that the Guile build system is ;; not deterministic when parallel-build is enabled. #f)))))) (package-with-relocatable-glibc (static-package guile)))) (define %guile-static-stripped ;; A stripped static Guile binary, for use during bootstrap. (package (inherit %guile-static) (name "guile-static-stripped") (build-system trivial-build-system) (arguments ;; The end result should depend on nothing but itself. `(#:allowed-references ("out") #:modules ((guix build utils)) #:builder (let () (use-modules (guix build utils)) (let* ((in (assoc-ref %build-inputs "guile")) (out (assoc-ref %outputs "out")) (guile1 (string-append in "/bin/guile")) (guile2 (string-append out "/bin/guile"))) (mkdir-p (string-append out "/share/guile/2.2")) (copy-recursively (string-append in "/share/guile/2.2") (string-append out "/share/guile/2.2")) (mkdir-p (string-append out "/lib/guile/2.2/ccache")) (copy-recursively (string-append in "/lib/guile/2.2/ccache") (string-append out "/lib/guile/2.2/ccache")) (mkdir (string-append out "/bin")) (copy-file guile1 guile2) ;; Verify that the relocated Guile works. ,@(if (%current-target-system) '() '((invoke guile2 "--version"))) ;; Strip store references. (remove-store-references guile2) ;; Verify that the stripped Guile works. If it aborts, it could be ;; that it tries to open iconv descriptors and fails because libc's ;; iconv data isn't available (see `guile-default-utf8.patch'.) ,@(if (%current-target-system) '() '((invoke guile2 "--version"))) #t)))) (inputs `(("guile" ,%guile-static))) (outputs '("out")) (synopsis "Minimal statically-linked and relocatable Guile"))) (define (tarball-package pkg) "Return a package containing a tarball of PKG." (package (inherit pkg) (name (string-append (package-name pkg) "-tarball")) (build-system trivial-build-system) (native-inputs `(("tar" ,tar) ("xz" ,xz))) (inputs `(("input" ,pkg))) (arguments (let ((name (package-name pkg)) (version (package-version pkg))) `(#:modules ((guix build utils)) #:builder (begin (use-modules (guix build utils)) (let ((out (assoc-ref %outputs "out")) (input (assoc-ref %build-inputs "input")) (tar (assoc-ref %build-inputs "tar")) (xz (assoc-ref %build-inputs "xz"))) (mkdir out) (set-path-environment-variable "PATH" '("bin") (list tar xz)) (with-directory-excursion input (invoke "tar" "cJvf" (string-append out "/" ,name "-" ,version "-" ,(or (%current-target-system) (%current-system)) ".tar.xz") "." ;; avoid non-determinism in the archive "--sort=name" "--mtime=@0" "--owner=root:0" "--group=root:0"))))))))) (define %bootstrap-binaries-tarball ;; A tarball with the statically-linked bootstrap binaries. (tarball-package %static-binaries)) (define %linux-libre-headers-bootstrap-tarball ;; A tarball with the statically-linked Linux-Libre-Headers programs. (tarball-package %linux-libre-headers-stripped)) (define %binutils-bootstrap-tarball ;; A tarball with the statically-linked Binutils programs. (tarball-package %binutils-static-stripped)) (define (%glibc-bootstrap-tarball) ;; A tarball with GNU libc's shared libraries, dynamic linker, and headers. (tarball-package (%glibc-stripped))) (define %gcc-bootstrap-tarball ;; A tarball with a dynamic-linked GCC and its headers. (tarball-package %gcc-stripped)) (define %guile-bootstrap-tarball ;; A tarball with the statically-linked, relocatable Guile. (tarball-package %guile-static-stripped)) (define %mescc-tools-bootstrap-tarball ;; A tarball with statically-linked MesCC binary seed. (tarball-package %mescc-tools-static-stripped)) (define %mes-bootstrap-tarball ;; A tarball with Mes binary seed. (tarball-package %mes-minimal-stripped)) (define %bootstrap-tarballs ;; A single derivation containing all the bootstrap tarballs, for ;; convenience. (package (name "bootstrap-tarballs") (version "0") (source #f) (build-system trivial-build-system) (arguments `(#:modules ((guix build utils)) #:builder (let ((out (assoc-ref %outputs "out"))) (use-modules (guix build utils) (ice-9 match) (srfi srfi-26)) (setvbuf (current-output-port) _IOLBF) (mkdir out) (chdir out) (for-each (match-lambda ((name . directory) (for-each (lambda (file) (format #t "~a -> ~a~%" file out) (symlink file (basename file))) (find-files directory "\\.tar\\.")))) %build-inputs) #t))) (inputs `(("guile-tarball" ,%guile-bootstrap-tarball) ,@(match (or (%current-target-system) (%current-system)) ((or "i686-linux" "x86_64-linux") `(("bootstrap-mescc-tools" ,%mescc-tools-bootstrap-tarball) ("bootstrap-mes" ,%mes-bootstrap-tarball) ("bootstrap-linux-libre-headers" ,%linux-libre-headers-bootstrap-tarball))) (_ `(("gcc-tarball" ,%gcc-bootstrap-tarball) ("binutils-tarball" ,%binutils-bootstrap-tarball) ("glibc-tarball" ,(%glibc-bootstrap-tarball))))) ("coreutils&co-tarball" ,%bootstrap-binaries-tarball))) (synopsis "Tarballs containing all the bootstrap binaries") (description synopsis) (home-page #f) (license gpl3+))) ;;; make-bootstrap.scm ends here