diff options
| -rw-r--r-- | doc/guix.texi | 26 | ||||
| -rw-r--r-- | guix/scripts/environment.scm | 32 | ||||
| -rw-r--r-- | tests/guix-environment-container.sh | 18 | ||||
| -rw-r--r-- | tests/guix-shell.sh | 3 |
4 files changed, 72 insertions, 7 deletions
diff --git a/doc/guix.texi b/doc/guix.texi index b2ff12dcb11..50eddc426e3 100644 --- a/doc/guix.texi +++ b/doc/guix.texi | |||
| @@ -6591,6 +6591,21 @@ directory within the container. If this is undesirable, | |||
| 6591 | be automatically shared and will change to the user's home directory | 6591 | be automatically shared and will change to the user's home directory |
| 6592 | within the container instead. See also @option{--user}. | 6592 | within the container instead. See also @option{--user}. |
| 6593 | 6593 | ||
| 6594 | @item --cwd=@var{directory} | ||
| 6595 | For containers, change to @var{directory} inside the container instead | ||
| 6596 | of the default location. The default location is the current working | ||
| 6597 | directory when @option{--no-cwd} is not used, or the user's home | ||
| 6598 | directory when @option{--no-cwd} is used. When @var{directory} is a | ||
| 6599 | relative file name, it is interpreted relative to the current working | ||
| 6600 | directory on the host. Note that @option{--cwd} does not automatically | ||
| 6601 | share @var{directory}---you must ensure it is accessible via the default | ||
| 6602 | current directory sharing or explicit @option{--share}/@option{--expose} | ||
| 6603 | options. For example: | ||
| 6604 | |||
| 6605 | @example | ||
| 6606 | guix shell --container --cwd=/tmp --share=/tmp coreutils | ||
| 6607 | @end example | ||
| 6608 | |||
| 6594 | @item --writable-root | 6609 | @item --writable-root |
| 6595 | When using @option{--container}, this option makes the root file system | 6610 | When using @option{--container}, this option makes the root file system |
| 6596 | writable (it is read-only by default). | 6611 | writable (it is read-only by default). |
| @@ -7092,6 +7107,17 @@ directory within the container. If this is undesirable, | |||
| 7092 | be automatically shared and will change to the user's home directory | 7107 | be automatically shared and will change to the user's home directory |
| 7093 | within the container instead. See also @option{--user}. | 7108 | within the container instead. See also @option{--user}. |
| 7094 | 7109 | ||
| 7110 | @item --cwd=@var{directory} | ||
| 7111 | For containers, change to @var{directory} inside the container instead | ||
| 7112 | of the default location. The default location is the current working | ||
| 7113 | directory when @option{--no-cwd} is not used, or the user's home | ||
| 7114 | directory when @option{--no-cwd} is used. When @var{directory} is a | ||
| 7115 | relative file name, it is interpreted relative to the current working | ||
| 7116 | directory on the host. Note that @option{--cwd} does not automatically | ||
| 7117 | share @var{directory}---you must ensure it is accessible via the default | ||
| 7118 | current directory sharing or explicit @option{--share}/@option{--expose} | ||
| 7119 | options. | ||
| 7120 | |||
| 7095 | @item --expose=@var{source}[=@var{target}] | 7121 | @item --expose=@var{source}[=@var{target}] |
| 7096 | @itemx --share=@var{source}[=@var{target}] | 7122 | @itemx --share=@var{source}[=@var{target}] |
| 7097 | For containers, @option{--expose} (resp. @option{--share}) exposes the | 7123 | For containers, @option{--expose} (resp. @option{--share}) exposes the |
diff --git a/guix/scripts/environment.scm b/guix/scripts/environment.scm index 86c8e31def9..8e5d76bce7d 100644 --- a/guix/scripts/environment.scm +++ b/guix/scripts/environment.scm | |||
| @@ -123,6 +123,10 @@ shell'." | |||
| 123 | --no-cwd do not share current working directory with an | 123 | --no-cwd do not share current working directory with an |
| 124 | isolated container")) | 124 | isolated container")) |
| 125 | (display (G_ " | 125 | (display (G_ " |
| 126 | --cwd=DIR change to DIRECTORY inside the container (default is | ||
| 127 | current working directory--or $HOME if --no-cwd is | ||
| 128 | used)")) | ||
| 129 | (display (G_ " | ||
| 126 | --writable-root make the container's root file system writable")) | 130 | --writable-root make the container's root file system writable")) |
| 127 | 131 | ||
| 128 | (display (G_ " | 132 | (display (G_ " |
| @@ -265,6 +269,9 @@ use '--preserve' instead~%")) | |||
| 265 | (option '("no-cwd") #f #f | 269 | (option '("no-cwd") #f #f |
| 266 | (lambda (opt name arg result) | 270 | (lambda (opt name arg result) |
| 267 | (alist-cons 'no-cwd? #t result))) | 271 | (alist-cons 'no-cwd? #t result))) |
| 272 | (option '("cwd") #t #f | ||
| 273 | (lambda (opt name arg result) | ||
| 274 | (alist-cons 'cwd arg result))) | ||
| 268 | (option '("writable-root") #f #f | 275 | (option '("writable-root") #f #f |
| 269 | (lambda (opt name arg result) | 276 | (lambda (opt name arg result) |
| 270 | (alist-cons 'writable-root? #t result))) | 277 | (alist-cons 'writable-root? #t result))) |
| @@ -743,7 +750,7 @@ regexps in WHITE-LIST." | |||
| 743 | 750 | ||
| 744 | (define* (launch-environment/container #:key command bash user user-mappings | 751 | (define* (launch-environment/container #:key command bash user user-mappings |
| 745 | profile manifest link-profile? network? | 752 | profile manifest link-profile? network? |
| 746 | map-cwd? emulate-fhs? nesting? | 753 | map-cwd? cwd emulate-fhs? nesting? |
| 747 | writable-root? | 754 | writable-root? |
| 748 | (setup-hook #f) | 755 | (setup-hook #f) |
| 749 | (symlinks '()) (white-list '())) | 756 | (symlinks '()) (white-list '())) |
| @@ -825,7 +832,7 @@ WHILE-LIST." | |||
| 825 | (inputs->requisites | 832 | (inputs->requisites |
| 826 | (list (direct-store-path bash) profile))))) | 833 | (list (direct-store-path bash) profile))))) |
| 827 | (return | 834 | (return |
| 828 | (let* ((cwd (getcwd)) | 835 | (let* ((host-cwd (getcwd)) ;actual current working directory |
| 829 | (home (getenv "HOME")) | 836 | (home (getenv "HOME")) |
| 830 | (uid (if user 1000 (getuid))) | 837 | (uid (if user 1000 (getuid))) |
| 831 | (gid (if user | 838 | (gid (if user |
| @@ -857,6 +864,13 @@ WHILE-LIST." | |||
| 857 | (name "overflow")))) | 864 | (name "overflow")))) |
| 858 | (home-dir (password-entry-directory passwd)) | 865 | (home-dir (password-entry-directory passwd)) |
| 859 | (logname (password-entry-name passwd)) | 866 | (logname (password-entry-name passwd)) |
| 867 | (container-cwd | ||
| 868 | (and cwd | ||
| 869 | (override-user-dir | ||
| 870 | user home | ||
| 871 | (if (string-prefix? "/" cwd) | ||
| 872 | cwd | ||
| 873 | (string-append host-cwd "/" cwd))))) | ||
| 860 | (environ (filter (match-lambda | 874 | (environ (filter (match-lambda |
| 861 | ((variable . value) | 875 | ((variable . value) |
| 862 | (find (cut regexp-exec <> variable) | 876 | (find (cut regexp-exec <> variable) |
| @@ -873,8 +887,8 @@ WHILE-LIST." | |||
| 873 | ;; Share current working directory, unless asked not to. | 887 | ;; Share current working directory, unless asked not to. |
| 874 | (if map-cwd? | 888 | (if map-cwd? |
| 875 | (list (file-system-mapping | 889 | (list (file-system-mapping |
| 876 | (source cwd) | 890 | (source host-cwd) |
| 877 | (target cwd) | 891 | (target host-cwd) |
| 878 | (writable? #t))) | 892 | (writable? #t))) |
| 879 | '()) | 893 | '()) |
| 880 | ;; Add the user mappings *after* the current working directory | 894 | ;; Add the user mappings *after* the current working directory |
| @@ -942,9 +956,10 @@ WHILE-LIST." | |||
| 942 | 956 | ||
| 943 | ;; For convenience, start in the user's current working | 957 | ;; For convenience, start in the user's current working |
| 944 | ;; directory or, if unmapped, the home directory. | 958 | ;; directory or, if unmapped, the home directory. |
| 945 | (chdir (if map-cwd? | 959 | (chdir (or container-cwd ; explicit --cwd takes precedence |
| 946 | (override-user-dir user home cwd) | 960 | (and map-cwd? ; otherwise, use current dir unless --no-cwd |
| 947 | home-dir)) | 961 | (override-user-dir user home host-cwd)) |
| 962 | home-dir)) ; fallback to home | ||
| 948 | 963 | ||
| 949 | ;; Set environment variables that match WHITE-LIST. | 964 | ;; Set environment variables that match WHITE-LIST. |
| 950 | (for-each (match-lambda | 965 | (for-each (match-lambda |
| @@ -1187,6 +1202,8 @@ command-line option processing with 'parse-command-line'." | |||
| 1187 | (leave (G_ "'--link-profile' cannot be used without '--container'~%"))) | 1202 | (leave (G_ "'--link-profile' cannot be used without '--container'~%"))) |
| 1188 | (when user | 1203 | (when user |
| 1189 | (leave (G_ "'--user' cannot be used without '--container'~%"))) | 1204 | (leave (G_ "'--user' cannot be used without '--container'~%"))) |
| 1205 | (when (assoc-ref opts 'cwd) | ||
| 1206 | (leave (G_ "'--cwd' cannot be used without '--container'~%"))) | ||
| 1190 | (when no-cwd? | 1207 | (when no-cwd? |
| 1191 | (leave (G_ "--no-cwd cannot be used without '--container'~%"))) | 1208 | (leave (G_ "--no-cwd cannot be used without '--container'~%"))) |
| 1192 | (when writable-root? | 1209 | (when writable-root? |
| @@ -1278,6 +1295,7 @@ when using '--container'; doing nothing~%")) | |||
| 1278 | #:link-profile? link-prof? | 1295 | #:link-profile? link-prof? |
| 1279 | #:network? network? | 1296 | #:network? network? |
| 1280 | #:map-cwd? (not no-cwd?) | 1297 | #:map-cwd? (not no-cwd?) |
| 1298 | #:cwd (assoc-ref opts 'cwd) | ||
| 1281 | #:writable-root? writable-root? | 1299 | #:writable-root? writable-root? |
| 1282 | #:emulate-fhs? emulate-fhs? | 1300 | #:emulate-fhs? emulate-fhs? |
| 1283 | #:nesting? nesting? | 1301 | #:nesting? nesting? |
diff --git a/tests/guix-environment-container.sh b/tests/guix-environment-container.sh index 5b89f8383b6..8947a4ea1ba 100644 --- a/tests/guix-environment-container.sh +++ b/tests/guix-environment-container.sh | |||
| @@ -186,6 +186,24 @@ HOME="$tmpdir" guix environment --bootstrap --container --user=foognu \ | |||
| 186 | -- /bin/sh -c 'test $(pwd) == "/home/foo" -a ! -d '"$tmpdir" | 186 | -- /bin/sh -c 'test $(pwd) == "/home/foo" -a ! -d '"$tmpdir" |
| 187 | ) | 187 | ) |
| 188 | 188 | ||
| 189 | # '--cwd' is independent from sharing the host current working directory. | ||
| 190 | ( | ||
| 191 | cd "$tmpdir" \ | ||
| 192 | && guix environment --bootstrap --container --no-cwd --cwd=/tmp \ | ||
| 193 | --ad-hoc guile-bootstrap --pure \ | ||
| 194 | -- /bin/sh -c 'test $(pwd) == "/tmp" -a ! -d '"$tmpdir" | ||
| 195 | ) | ||
| 196 | |||
| 197 | # Relative '--cwd' is resolved against the host CWD and still honors '--user'. | ||
| 198 | mkdir -p "$tmpdir/home/wd" | ||
| 199 | home_dir="$(cd "$tmpdir/home"; pwd -P)" | ||
| 200 | ( | ||
| 201 | cd "$tmpdir/home/wd" \ | ||
| 202 | && HOME="$home_dir" guix environment --bootstrap --container --user=foo \ | ||
| 203 | --cwd=. --ad-hoc guile-bootstrap --pure \ | ||
| 204 | -- /bin/sh -c 'test "$(pwd)" = "/home/foo/wd"' | ||
| 205 | ) | ||
| 206 | |||
| 189 | # Check that the root file system is read-only by default... | 207 | # Check that the root file system is read-only by default... |
| 190 | guix environment --bootstrap --container --ad-hoc guile-bootstrap \ | 208 | guix environment --bootstrap --container --ad-hoc guile-bootstrap \ |
| 191 | -- guile -c '(mkdir "/whatever")' && false | 209 | -- guile -c '(mkdir "/whatever")' && false |
diff --git a/tests/guix-shell.sh b/tests/guix-shell.sh index b2f820bf26d..4eb4a3196cd 100644 --- a/tests/guix-shell.sh +++ b/tests/guix-shell.sh | |||
| @@ -35,6 +35,9 @@ guix shell --bootstrap --pure guile-bootstrap -- guile --version | |||
| 35 | # '--symlink' can only be used with --container. | 35 | # '--symlink' can only be used with --container. |
| 36 | guix shell --bootstrap guile-bootstrap -S /dummy=bin/guile && false | 36 | guix shell --bootstrap guile-bootstrap -S /dummy=bin/guile && false |
| 37 | 37 | ||
| 38 | # '--cwd' can only be used with --container. | ||
| 39 | guix shell --cwd=/tmp -n hello && false | ||
| 40 | |||
| 38 | # '--ad-hoc' is a thing of the past. | 41 | # '--ad-hoc' is a thing of the past. |
| 39 | guix shell --ad-hoc guile-bootstrap && false | 42 | guix shell --ad-hoc guile-bootstrap && false |
| 40 | 43 | ||
