all messages for Guix-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* [bug#57031] [PATCH] scripts: Show a hint the first time some commands are run as root.
@ 2022-08-07 11:14 ( via Guix-patches via
  2022-08-07 11:19 ` [bug#57031] [PATCH v2] " ( via Guix-patches via
                   ` (2 more replies)
  0 siblings, 3 replies; 13+ messages in thread
From: ( via Guix-patches via @ 2022-08-07 11:14 UTC (permalink / raw)
  To: 57031; +Cc: (

* guix/scripts/package.scm (guix-package*): Add `#:root-hint?` keyword
  argument. Display a hint if Guix is being run as root and `root-hint?`
  is #t.
* guix/scripts/install.scm (guix-install): Use `#:root-hint? #t` here...
* guix/scripts/remove.scm (guix-remove): ...here...
* guix/scripts/upgrade.scm (guix-upgrade): ...and here.
* guix/scripts/pull.scm (guix-pull): (guix-pull): Display a hint if
  Guix is being run as root.

A pretty common beginner mistake, it seems, is assuming that since
every other package manager you've used requires root for installing,
removing, and upgrading packages, Guix must too.

This commit tries to make it harder to make such an assumption, by
making commands such as `pull`, `package`, and `upgrade` display
a hint the first time they are run as root.
---
 guix/scripts/install.scm |  3 ++-
 guix/scripts/package.scm | 16 +++++++++++++---
 guix/scripts/pull.scm    |  7 +++++++
 guix/scripts/remove.scm  |  3 ++-
 guix/scripts/upgrade.scm |  3 ++-
 guix/ui.scm              | 29 +++++++++++++++++++++++++++++
 6 files changed, 55 insertions(+), 6 deletions(-)

diff --git a/guix/scripts/install.scm b/guix/scripts/install.scm
index 63e625f266..bf11fc7b11 100644
--- a/guix/scripts/install.scm
+++ b/guix/scripts/install.scm
@@ -1,5 +1,6 @@
 ;;; GNU Guix --- Functional package management for GNU
 ;;; Copyright © 2019, 2020 Ludovic Courtès <ludo@gnu.org>
+;;; Copyright © 2022 ( <paren@disroot.org>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -80,4 +81,4 @@ (define opts
                         (list %package-default-options #f)
                         #:argument-handler handle-argument))
 
-  (guix-package* opts))
+  (guix-package* opts #:root-hint? #t))
diff --git a/guix/scripts/package.scm b/guix/scripts/package.scm
index 7d92598efa..8936c70e1f 100644
--- a/guix/scripts/package.scm
+++ b/guix/scripts/package.scm
@@ -12,6 +12,7 @@
 ;;; Copyright © 2018 Steve Sprang <scs@stevesprang.com>
 ;;; Copyright © 2022 Josselin Poiret <dev@jpoiret.xyz>
 ;;; Copyright © 2022 Antero Mejr <antero@mailbox.org>
+;;; Copyright © 2022 ( <paren@disroot.org>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -1077,12 +1078,21 @@ (define opts
     (parse-command-line args %options (list %default-options #f)
                         #:argument-handler handle-argument))
 
-  (guix-package* opts))
+  (guix-package* opts #:root-hint? #t))
 
-(define (guix-package* opts)
+(define* (guix-package* opts #:key (root-hint? #f))
   "Run the 'guix package' command on OPTS, an alist resulting for command-line
-option processing with 'parse-command-line'."
+option processing with 'parse-command-line'.  If ROOT-HINT? is #T, a hint is
+shown on the first usage of this procedure that informs users about Guix's
+support for per-user package management."
   (with-error-handling
+    (when (and root-hint?
+               (not (hint-given? 'package-root-hint))
+               (= (getuid) 0))
+      (record-hint 'package-root-hint)
+      (display-hint (G_ "`guix package' is user-specific, not system-wide,
+so running this command as root will affect only the `root' user.")))
+
     (or (process-query opts)
         (parameterize ((%store  (open-connection))
                        (%graft? (assoc-ref opts 'graft?)))
diff --git a/guix/scripts/pull.scm b/guix/scripts/pull.scm
index b0cc459d63..6e3d7db7c7 100644
--- a/guix/scripts/pull.scm
+++ b/guix/scripts/pull.scm
@@ -2,6 +2,7 @@
 ;;; Copyright © 2013-2015, 2017-2022 Ludovic Courtès <ludo@gnu.org>
 ;;; Copyright © 2017 Marius Bakke <mbakke@fastmail.com>
 ;;; Copyright © 2020, 2021 Tobias Geerinckx-Rice <me@tobias.gr>
+;;; Copyright © 2022 ( <paren@disroot.org>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -834,6 +835,12 @@ (define (no-arguments arg _)
             (current-channels (profile-channels profile))
             (validate-pull    (assoc-ref opts 'validate-pull))
             (authenticate?    (assoc-ref opts 'authenticate-channels?)))
+       (when (and (not (hint-given? 'pull-root-hint))
+                  (= (getuid) 0))
+         (record-hint 'pull-root-hint)
+         (display-hint (G_ "`guix pull' is user-specific, not system-wide;
+running it as root will only affect the `root' user.")))
+
        (cond
         ((assoc-ref opts 'query)
          (process-query opts profile))
diff --git a/guix/scripts/remove.scm b/guix/scripts/remove.scm
index a46ad04d56..131649eace 100644
--- a/guix/scripts/remove.scm
+++ b/guix/scripts/remove.scm
@@ -1,5 +1,6 @@
 ;;; GNU Guix --- Functional package management for GNU
 ;;; Copyright © 2019, 2020 Ludovic Courtès <ludo@gnu.org>
+;;; Copyright © 2022 ( <paren@disroot.org>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -76,4 +77,4 @@ (define opts
                         (list %package-default-options #f)
                         #:argument-handler handle-argument))
 
-  (guix-package* opts))
+  (guix-package* opts #:root-hint? #t))
diff --git a/guix/scripts/upgrade.scm b/guix/scripts/upgrade.scm
index beb59cbe6f..dd14600fe4 100644
--- a/guix/scripts/upgrade.scm
+++ b/guix/scripts/upgrade.scm
@@ -2,6 +2,7 @@
 ;;; Copyright © 2019, 2020 Ludovic Courtès <ludo@gnu.org>
 ;;; Copyright © 2020 Jakub Kądziołka <kuba@kadziolka.net>
 ;;; Copyright © 2020 Simon Tournier <zimon.toutoune@gmail.com>
+;;; Copyright © 2022 ( <paren@disroot.org>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -87,4 +88,4 @@ (define opts
                               #f)
                         #:argument-handler handle-argument))
 
-  (guix-package* opts))
+  (guix-package* opts #:root-hint? #t))
diff --git a/guix/ui.scm b/guix/ui.scm
index a7acd41440..44607d80d2 100644
--- a/guix/ui.scm
+++ b/guix/ui.scm
@@ -18,6 +18,7 @@
 ;;; Copyright © 2020 Maxim Cournoyer <maxim.cournoyer@gmail.com>
 ;;; Copyright © 2018 Steve Sprang <scs@stevesprang.com>
 ;;; Copyright © 2022 Taiju HIGASHI <higashi@taiju.info>
+;;; Copyright © 2022 ( <paren@disroot.org>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -136,6 +137,11 @@ (define-module (guix ui)
             switch-to-generation*
             delete-generation*
 
+            hint-directory
+            hint-file
+            record-hint
+            hint-given?
+
             %default-message-language
             current-message-language
 
@@ -2032,6 +2038,29 @@ (define* (package-specification->name+version+output spec
                  (package-name->name+version name)))
     (values name version sub-drv)))
 
+\f
+;;;
+;;; One-time hints.
+;;;
+
+(define (hint-directory)
+  "Return the directory name where previously given hints are recorded."
+  (string-append (cache-directory #:ensure? #f) "/hints"))
+
+(define (hint-file hint)
+  "Return the name of the file that marks HINT as already printed."
+  (string-append (hint-directory) "/" (symbol->string hint)))
+
+(define (record-hint hint)
+  "Mark HINT as already given."
+  (let ((file (hint-file hint)))
+    (mkdir-p (dirname file))
+    (close-fdes (open-fdes file (logior O_CREAT O_WRONLY)))))
+
+(define (hint-given? hint)
+  "Return true if HINT was already given."
+  (file-exists? (hint-file hint)))
+
 \f
 ;;;
 ;;; Command-line option processing.
-- 
2.37.1





^ permalink raw reply related	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2022-10-28 14:46 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-07 11:14 [bug#57031] [PATCH] scripts: Show a hint the first time some commands are run as root ( via Guix-patches via
2022-08-07 11:19 ` [bug#57031] [PATCH v2] " ( via Guix-patches via
2022-08-07 11:22   ` ( via Guix-patches via
2022-09-05 21:11   ` [bug#57031] [PATCH] " Ludovic Courtès
2022-09-11 13:05     ` ( via Guix-patches via
2022-09-11 19:59 ` [bug#57031] [PATCH v2 1/2] ui: Make one-time hint API public ( via Guix-patches via
2022-09-11 19:59   ` [bug#57031] [PATCH v2 2/2] scripts: Warn the first time pull or package is run as root ( via Guix-patches via
2022-10-26 18:14     ` zimoun
2022-10-27  5:52       ` ( via Guix-patches via
2022-10-27  8:23         ` zimoun
2022-10-28 14:33           ` ( via Guix-patches via
2022-10-27 19:42 ` [bug#57031] [PATCH v3 1/2] ui: Make one-time hint API public ( via Guix-patches via
2022-10-27 19:42   ` [bug#57031] [PATCH v3 2/2] scripts: Warn the first time pull or package is run as root ( via Guix-patches via

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/guix.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.