diff mbox series

[bug#50967,3/3] guix: scripts: Make sure profile directory exists.

Message ID 878rz54xmy.fsf@gmail.com
State Accepted
Headers show
Series None | expand

Commit Message

Oleg Pykhalov Oct. 6, 2021, 10:05 p.m. UTC
This patch fixes user's files corruption.  Also, I applied suggestions
about commit message, thanks zimoun!

Comments

Ludovic Courtès Oct. 8, 2021, 10:57 p.m. UTC | #1
Oleg Pykhalov <go.wigust@gmail.com> skribis:

> From eec2ab5f514ec6e156244898f8a635baf323ab70 Mon Sep 17 00:00:00 2001
> From: Oleg Pykhalov <go.wigust@gmail.com>
> Date: Tue, 5 Oct 2021 02:10:25 +0300
> Subject: [PATCH] scripts: home: Make sure profile directory exists.
>
> * guix/scripts/home.scm (process-action): Make sure profile directory exists.
> * tests/guix-home.sh: New file.
> * Makefile.am (SH_TESTS): Add this.

IMO the home.scm change should perhaps be a separate commit because it’s
not strictly what the test exercises.

> +++ b/tests/guix-home.sh
> @@ -0,0 +1,86 @@
> +#!/usr/bin/env bash
> +

You can remove these two lines.

> +run_guix_home_without_home()
> +{
> +    (
> +        # Save current directory (Guix source directory) for later mount
> +        test_directory="$(mktemp -d)"
> +        trap 'umount $test_directory; rmdir $test_directory' EXIT
> +        mount --bind "$PWD" "$test_directory"
> +
> +        # Make $HOME directory empty to preserve user's files
> +        mount --types tmpfs none "$HOME"
> +
> +        # Mount Guix source directory in a clean environment
> +        mkdir -p "$PWD"
> +        mount --bind "$test_directory" "$PWD"
> +        cd "$PWD" || exit 77

How about ‘set -e’ to make sure we abort as soon as things go wrong?

Also, wouldn’t it be easier to set HOME to a new directory?

> +        guix home reconfigure "${test_directory}/home.scm"

Looks like the test is not checking anything after ‘reconfigure’ has
completed.  Should it check for the presence of certain things in
~/.bashrc, for the presence of ~/.guix-home, the output of ‘guix home
describe’ maybe?

> +unshare -mrf bash -c "run_guix_home_without_home"

If we keep this strategy (rather than setting HOME), we need a check for
unprivileged user namespace support.  In
‘tests/guix-pack-relocatable.sh’, this is done by invoking “unshare -r
true” and checking the return value.

Could you send an updated patch?

Thanks,
Ludo’.
diff mbox series

Patch

From eec2ab5f514ec6e156244898f8a635baf323ab70 Mon Sep 17 00:00:00 2001
From: Oleg Pykhalov <go.wigust@gmail.com>
Date: Tue, 5 Oct 2021 02:10:25 +0300
Subject: [PATCH] scripts: home: Make sure profile directory exists.

* guix/scripts/home.scm (process-action): Make sure profile directory exists.
* tests/guix-home.sh: New file.
* Makefile.am (SH_TESTS): Add this.
---
 Makefile.am           |  1 +
 guix/scripts/home.scm |  2 +
 tests/guix-home.sh    | 86 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 89 insertions(+)
 create mode 100644 tests/guix-home.sh

diff --git a/Makefile.am b/Makefile.am
index b66789fa0b..5bf2567dc8 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -546,6 +546,7 @@  SH_TESTS =					\
   tests/guix-package-aliases.sh			\
   tests/guix-package-net.sh			\
   tests/guix-system.sh				\
+  tests/guix-home.sh				\
   tests/guix-archive.sh				\
   tests/guix-authenticate.sh			\
   tests/guix-environment.sh			\
diff --git a/guix/scripts/home.scm b/guix/scripts/home.scm
index 75df6d707d..115dfadb57 100644
--- a/guix/scripts/home.scm
+++ b/guix/scripts/home.scm
@@ -1,6 +1,7 @@ 
 ;;; GNU Guix --- Functional package management for GNU
 ;;; Copyright © 2021 Andrew Tropin <andrew@trop.in>
 ;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
+;;; Copyright © 2021 Oleg Pykhalov <go.wigust@gmail.com>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -179,6 +180,7 @@  ACTION must be one of the sub-commands that takes a home environment
 declaration as an argument (a file name.)  OPTS is the raw alist of options
 resulting from command-line parsing."
   (define (ensure-home-environment file-or-exp obj)
+    (ensure-profile-directory)
     (unless (home-environment? obj)
       (leave (G_ "'~a' does not return a home environment ~%")
              file-or-exp))
diff --git a/tests/guix-home.sh b/tests/guix-home.sh
new file mode 100644
index 0000000000..96cbd12857
--- /dev/null
+++ b/tests/guix-home.sh
@@ -0,0 +1,86 @@ 
+#!/usr/bin/env bash
+
+# GNU Guix --- Functional package management for GNU
+# Copyright © 2021 Oleg Pykhalov <go.wigust@gmail.com>
+#
+# 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/>.
+
+#
+# Test the 'guix home' using the external store, if any.
+#
+
+guix home --version
+
+NIX_STORE_DIR="$(guile -c '(use-modules (guix config))(display %storedir)')"
+localstatedir="$(guile -c '(use-modules (guix config))(display %localstatedir)')"
+GUIX_DAEMON_SOCKET="$localstatedir/guix/daemon-socket/socket"
+export NIX_STORE_DIR GUIX_DAEMON_SOCKET
+
+# Run tests only when a "real" daemon is available.
+if ! guile -c '(use-modules (guix)) (exit (false-if-exception (open-connection)))'
+then
+    exit 77
+fi
+
+STORE_PARENT="$(dirname "$NIX_STORE_DIR")"
+export STORE_PARENT
+if test "$STORE_PARENT" = "/"; then exit 77; fi
+
+run_guix_home_without_home()
+{
+    (
+        # Save current directory (Guix source directory) for later mount
+        test_directory="$(mktemp -d)"
+        trap 'umount $test_directory; rmdir $test_directory' EXIT
+        mount --bind "$PWD" "$test_directory"
+
+        # Make $HOME directory empty to preserve user's files
+        mount --types tmpfs none "$HOME"
+
+        # Mount Guix source directory in a clean environment
+        mkdir -p "$PWD"
+        mount --bind "$test_directory" "$PWD"
+        cd "$PWD" || exit 77
+
+        cat > "dot-bashrc" <<'EOF'
+# dot-bashrc test file for guix home
+EOF
+
+        cat > "home.scm" <<'EOF'
+(use-modules (gnu home)
+             (gnu home-services)
+             (gnu home services shells)
+             (gnu services)
+             (guix gexp))
+
+(home-environment
+ (services
+  (list
+   (service home-bash-service-type
+            (home-bash-configuration
+             (guix-defaults? #t)
+             (bashrc
+              (list
+               (local-file (string-append (dirname (current-filename))
+                                          "/dot-bashrc")))))))))
+EOF
+
+        guix home reconfigure "${test_directory}/home.scm"
+    )
+}
+export -f run_guix_home_without_home
+
+unshare -mrf bash -c "run_guix_home_without_home"
-- 
2.33.0