From patchwork Sun Mar 13 21:54:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Ludovic_Court=C3=A8s?= X-Patchwork-Id: 37781 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id DFA8B27BBEA; Sun, 13 Mar 2022 21:56:34 +0000 (GMT) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-3.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL, SPF_HELO_PASS autolearn=unavailable autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id A256727BBE9 for ; Sun, 13 Mar 2022 21:56:34 +0000 (GMT) Received: from localhost ([::1]:45842 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nTWCX-0002D8-P0 for patchwork@mira.cbaines.net; Sun, 13 Mar 2022 17:56:33 -0400 Received: from eggs.gnu.org ([209.51.188.92]:36260) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nTWC2-0002By-IX for guix-patches@gnu.org; Sun, 13 Mar 2022 17:56:02 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:49573) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nTWC2-0007TB-A3 for guix-patches@gnu.org; Sun, 13 Mar 2022 17:56:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1nTWC2-0007rR-9b for guix-patches@gnu.org; Sun, 13 Mar 2022 17:56:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#54377] [PATCH 1/3] linux-container: 'eval/container' honors #:namespaces. References: <20220313215259.9394-1-ludo@gnu.org> In-Reply-To: <20220313215259.9394-1-ludo@gnu.org> Resent-From: Ludovic =?utf-8?q?Court=C3=A8s?= Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Sun, 13 Mar 2022 21:56:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 54377 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 54377@debbugs.gnu.org Cc: Ludovic =?utf-8?q?Court=C3=A8s?= Received: via spool by 54377-submit@debbugs.gnu.org id=B54377.164720850930131 (code B ref 54377); Sun, 13 Mar 2022 21:56:02 +0000 Received: (at 54377) by debbugs.gnu.org; 13 Mar 2022 21:55:09 +0000 Received: from localhost ([127.0.0.1]:43466 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nTWBA-0007pv-JN for submit@debbugs.gnu.org; Sun, 13 Mar 2022 17:55:08 -0400 Received: from eggs.gnu.org ([209.51.188.92]:58656) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nTWB8-0007pA-LL for 54377@debbugs.gnu.org; Sun, 13 Mar 2022 17:55:06 -0400 Received: from [2001:470:142:3::e] (port=34774 helo=fencepost.gnu.org) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nTWB3-00079D-BO; Sun, 13 Mar 2022 17:55:01 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=MIME-Version:Date:Subject:To:From:in-reply-to: references; bh=VFOrmGxBD7tSW0Plmrs4QGL3rd6UBOIdiASoqy8k1s0=; b=XKZodhOCWfvM93 vBBNZG3cPST7r5eM/bQZKI819VeUoyNyeu+YdZR+7om6PgBk1UOpP5VSWOvhWaTnXloF+oj8ZsCXt aHtBB4L8nX8D+qt+G5O2UkOX3XdeAQO9ITfBgB+Z+f+Fzl2WBWf92MmCQMMSpaq9Kptylka3h3xl4 9XYeuenSlVzEzcRsi4xObzqzBmZoBu0Qqc1wz4IAPsAs61c7UBeygy5PlOjdHCeOiotE0ifeImSrj 1Gt5Y+ZaeG+8ozOZpWVUzf/xVqyFomgGjkzMRVDabwFw5TCUzcyeIxLsR0zvTXgp0vdXVlXyW71kb SHj8h0CBkH/uf+ZX/imA==; Received: from 91-160-117-201.subs.proxad.net ([91.160.117.201]:62695 helo=gnu.org) by fencepost.gnu.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nTWB2-0003Co-T1; Sun, 13 Mar 2022 17:55:01 -0400 From: Ludovic =?utf-8?q?Court=C3=A8s?= Date: Sun, 13 Mar 2022 22:54:52 +0100 Message-Id: <20220313215454.9576-1-ludo@gnu.org> X-Mailer: git-send-email 2.34.0 MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: "Guix-patches" X-getmail-retrieved-from-mailbox: Patches * gnu/system/linux-container.scm (eval/container): Pass #:namespaces to 'call-with-container'. --- gnu/system/linux-container.scm | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/gnu/system/linux-container.scm b/gnu/system/linux-container.scm index e6fd0f1315..415d6b9775 100644 --- a/gnu/system/linux-container.scm +++ b/gnu/system/linux-container.scm @@ -291,4 +291,5 @@ (define items lowered)) (list "-c" (object->string - (lowered-gexp-sexp lowered)))))))))))) + (lowered-gexp-sexp lowered)))))) + #:namespaces namespaces)))))) From patchwork Sun Mar 13 21:54:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ludovic_Court=C3=A8s?= X-Patchwork-Id: 37782 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id DA61727BBEA; Sun, 13 Mar 2022 21:56:35 +0000 (GMT) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-3.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL, SPF_HELO_PASS autolearn=unavailable autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id A168727BBE9 for ; Sun, 13 Mar 2022 21:56:35 +0000 (GMT) Received: from localhost ([::1]:45892 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nTWCY-0002FI-QQ for patchwork@mira.cbaines.net; Sun, 13 Mar 2022 17:56:34 -0400 Received: from eggs.gnu.org ([209.51.188.92]:36262) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nTWC2-0002CL-Ut for guix-patches@gnu.org; Sun, 13 Mar 2022 17:56:02 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:49574) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nTWC2-0007TG-Mj for guix-patches@gnu.org; Sun, 13 Mar 2022 17:56:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1nTWC2-0007rY-ME for guix-patches@gnu.org; Sun, 13 Mar 2022 17:56:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#54377] [PATCH 2/3] linux-container: Add #:guest-uid and #:guest-gid to 'eval/container'. Resent-From: Ludovic =?utf-8?q?Court=C3=A8s?= Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Sun, 13 Mar 2022 21:56:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 54377 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 54377@debbugs.gnu.org Cc: Ludovic =?utf-8?q?Court=C3=A8s?= Received: via spool by 54377-submit@debbugs.gnu.org id=B54377.164720851130146 (code B ref 54377); Sun, 13 Mar 2022 21:56:02 +0000 Received: (at 54377) by debbugs.gnu.org; 13 Mar 2022 21:55:11 +0000 Received: from localhost ([127.0.0.1]:43468 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nTWBC-0007q9-TA for submit@debbugs.gnu.org; Sun, 13 Mar 2022 17:55:11 -0400 Received: from eggs.gnu.org ([209.51.188.92]:58666) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nTWB9-0007pO-73 for 54377@debbugs.gnu.org; Sun, 13 Mar 2022 17:55:07 -0400 Received: from [2001:470:142:3::e] (port=34776 helo=fencepost.gnu.org) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nTWB3-00079a-T6; Sun, 13 Mar 2022 17:55:01 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=MIME-Version:References:In-Reply-To:Date:Subject:To: From; bh=9+tFWJSQvSObmGAVOflTp472fIIrYqU8MzHz1aYfnyw=; b=my/x0ZgIIORku8KUYoGe H8VmB4Alvv13SfBoo1/4qMP0yMfniHQhCEIlBDwCXHNmCFElJ2zxnnV9mKnTCkFb8UpYGNm9xeRps 36C/MFYtRUipH1zV8yL+UV8kaokI6Mpx6acYFaLwdgjX96WC68EtGnfLT8dq89jKdtQLMwHmEz2Jv NrWifjwcGEG/SyuQyTZGRXv2fir2atGz60mFU1rOFO/Qed/4bsxA1BFM8ZaNDw0w4mKokrDFGE8ha L8nxokVk7AGgB3th8TjRO6k9/R/VWSOmd+8w8OMDA7wJ4JCe1lstOkW+L7U/56E8honOHfpX4N0xV rTQo/rSyvbgu9A==; Received: from 91-160-117-201.subs.proxad.net ([91.160.117.201]:62695 helo=gnu.org) by fencepost.gnu.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nTWB3-0003Co-FS; Sun, 13 Mar 2022 17:55:01 -0400 From: Ludovic =?utf-8?q?Court=C3=A8s?= Date: Sun, 13 Mar 2022 22:54:53 +0100 Message-Id: <20220313215454.9576-2-ludo@gnu.org> X-Mailer: git-send-email 2.34.0 In-Reply-To: <20220313215454.9576-1-ludo@gnu.org> References: <20220313215454.9576-1-ludo@gnu.org> MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: "Guix-patches" X-getmail-retrieved-from-mailbox: Patches * gnu/system/linux-container.scm (eval/container): Add #:guest-uid and #:guest-gid and honor them. --- gnu/system/linux-container.scm | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/gnu/system/linux-container.scm b/gnu/system/linux-container.scm index 415d6b9775..eeb0f68c02 100644 --- a/gnu/system/linux-container.scm +++ b/gnu/system/linux-container.scm @@ -1,6 +1,6 @@ ;;; GNU Guix --- Functional package management for GNU ;;; Copyright © 2015 David Thompson -;;; Copyright © 2016, 2017, 2019, 2020, 2021 Ludovic Courtès +;;; Copyright © 2016-2017, 2019-2022 Ludovic Courtès ;;; Copyright © 2019 Arun Isaac ;;; Copyright © 2020 Efraim Flashner ;;; Copyright © 2020 Google LLC @@ -248,11 +248,13 @@ (define (explain pid) (define* (eval/container exp #:key (mappings '()) - (namespaces %namespaces)) + (namespaces %namespaces) + (guest-uid 0) (guest-gid 0)) "Evaluate EXP, a gexp, in a new process executing in separate namespaces as listed in NAMESPACES. Add MAPPINGS, a list of , to the -set of directories visible in the process's mount namespace. Return the -process' exit status as a monadic value. +set of directories visible in the process's mount namespace. Inside the +namespaces, run code as GUEST-UID and GUEST-GID. Return the process' exit +status as a monadic value. This is useful to implement processes that, unlike derivations, are not entirely pure and need to access the outside world or to perform side @@ -292,4 +294,6 @@ (define items (list "-c" (object->string (lowered-gexp-sexp lowered)))))) - #:namespaces namespaces)))))) + #:namespaces namespaces + #:guest-uid guest-uid + #:guest-gid guest-gid)))))) From patchwork Sun Mar 13 21:54:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ludovic_Court=C3=A8s?= X-Patchwork-Id: 37783 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id B159327BBEA; Sun, 13 Mar 2022 21:57:14 +0000 (GMT) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-3.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL, SPF_HELO_PASS autolearn=unavailable autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id A203327BBE9 for ; Sun, 13 Mar 2022 21:57:12 +0000 (GMT) Received: from localhost ([::1]:47026 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nTWD9-00031V-Pi for patchwork@mira.cbaines.net; Sun, 13 Mar 2022 17:57:11 -0400 Received: from eggs.gnu.org ([209.51.188.92]:36264) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nTWC4-0002Cy-S5 for guix-patches@gnu.org; Sun, 13 Mar 2022 17:56:08 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:49575) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nTWC3-0007TN-2L for guix-patches@gnu.org; Sun, 13 Mar 2022 17:56:04 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1nTWC3-0007rg-1v for guix-patches@gnu.org; Sun, 13 Mar 2022 17:56:03 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#54377] [PATCH 3/3] guix home: Add 'container' command. Resent-From: Ludovic =?utf-8?q?Court=C3=A8s?= Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Sun, 13 Mar 2022 21:56:03 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 54377 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 54377@debbugs.gnu.org Cc: Ludovic =?utf-8?q?Court=C3=A8s?= Received: via spool by 54377-submit@debbugs.gnu.org id=B54377.164720851230158 (code B ref 54377); Sun, 13 Mar 2022 21:56:03 +0000 Received: (at 54377) by debbugs.gnu.org; 13 Mar 2022 21:55:12 +0000 Received: from localhost ([127.0.0.1]:43470 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nTWBD-0007qB-9U for submit@debbugs.gnu.org; Sun, 13 Mar 2022 17:55:12 -0400 Received: from eggs.gnu.org ([209.51.188.92]:58676) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nTWBA-0007pd-HA for 54377@debbugs.gnu.org; Sun, 13 Mar 2022 17:55:09 -0400 Received: from [2001:470:142:3::e] (port=34784 helo=fencepost.gnu.org) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nTWB5-0007Hf-8E; Sun, 13 Mar 2022 17:55:03 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=MIME-Version:References:In-Reply-To:Date:Subject:To: From; bh=tuqm1J+OmJYoUh6xyRIgl6+r7sWcFXfG0XtHj+ugrY0=; b=gCUI0eVnE+IUk05u43d9 miCjVLwDhjJjyjVOUoQg34G2G8RXepogu7/FtN9TmyifR3mquvpZVYw74s7c3mDXF9VybB2Wzd/+u AJ1WsgQjAx97ZlQBM0t8Ukg0rLLVQiUHSv0nnzpNBklFEJHmxfe54Y+IPImdc69ZI9nzxKGua6Znq NiEzzbPrYd3H8kld0cJOlB7M6JkLcRwXUtJr9OK/xSbxbFc6qnYUT1r6mqzM+MFYIoD56PBetf842 P6EWSt3OyYqXu3lEzrn04ujQgFsvr3LtNmHyq6NwuoC8j40nILk9fajPY1qzaGnWl/qTDhp0o7qcm 4CLPCOCCsAlwHQ==; Received: from 91-160-117-201.subs.proxad.net ([91.160.117.201]:62695 helo=gnu.org) by fencepost.gnu.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nTWB4-0003Co-2n; Sun, 13 Mar 2022 17:55:02 -0400 From: Ludovic =?utf-8?q?Court=C3=A8s?= Date: Sun, 13 Mar 2022 22:54:54 +0100 Message-Id: <20220313215454.9576-3-ludo@gnu.org> X-Mailer: git-send-email 2.34.0 In-Reply-To: <20220313215454.9576-1-ludo@gnu.org> References: <20220313215454.9576-1-ludo@gnu.org> MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: "Guix-patches" X-getmail-retrieved-from-mailbox: Patches * guix/scripts/home.scm (show-help, %options): Add '--network', '--share', and '--expose'. (not-config?, user-shell, spawn-home-container): New procedures. (%default-system-profile): New variable. (perform-action): Add #:file-system-mappings, #:container-command, and #:network?; honor them. (process-action): Adjust accordingly. (guix-home)[parse-sub-command]: Add "container". [parse-args]: New procedure. Use it instead of 'parse-command-line'. * tests/guix-home.sh: Add tests. * doc/guix.texi (Declaring the Home Environment): Mention 'guix home container' as a way to test configuration. (Invoking guix home): Document it. --- doc/guix.texi | 58 +++++++++ guix/scripts/home.scm | 271 ++++++++++++++++++++++++++++++++++++++---- tests/guix-home.sh | 58 ++++++--- 3 files changed, 349 insertions(+), 38 deletions(-) diff --git a/doc/guix.texi b/doc/guix.texi index 4b71fb7010..ba9199f336 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -38071,6 +38071,21 @@ be confused with Shepherd services (@pxref{Shepherd Services}). Using this exte mechanism and some Scheme code that glues things together gives the user the freedom to declare their own, very custom, home environments. +@cindex container, for @command{guix home} +Once the configuration looks good, you can first test it in a throw-away +``container'': + +@example +guix home container config.scm +@end example + +The command above spawns a shell where your home environment is running. +The shell runs in a container, meaning it's isolated from the rest of +the system, so it's a good way to try out your configuration---you can +see if configuration bits are missing or misbehaving, if daemons get +started, and so on. Once you exit that shell, you're back to the prompt +of your original shell ``in the real world''. + Once you have a configuration file that suits your needs, you can reconfigure your home by running: @@ -38699,6 +38714,49 @@ As for @command{guix search}, the result is written in @code{recutils} format, which makes it easy to filter the output (@pxref{Top, GNU recutils databases,, recutils, GNU recutils manual}). +@cindex container, for @command{guix home} +@item container +Spawn a shell in an isolated environment---a +@dfn{container}---containing your home as specified by @var{file}. + +For example, this is how you would start an interactive shell in a +container with your home: + +@example +guix home container config.scm +@end example + +This is a throw-away container where you can lightheartedly fiddle with +files; any changes made within the container, any process started---all +this disappears as soon as you exit that shell. + +As with @command{guix shell}, several options control that container: + +@table @option +@item --network +@itemx -N +Enable networking within the container (it is disabled by default). + +@item --expose=@var{source}[=@var{target}] +@itemx --share=@var{source}[=@var{target}] +As with @command{guix shell}, make directory @var{source} of the host +system available as @var{target} inside the container---read-only if you +pass @option{--expose}, and writable if you pass @option{--share} +(@pxref{Invoking guix shell, @option{--expose} and @option{--share}}). +@end table + +Additionally, you can run a command in that container, instead of +spawning an interactive shell. For instance, here is how you would +check which Shepherd services are started in a throw-away home +container: + +@example +guix home container config.scm -- herd status +@end example + +The command to run in the container must come after @code{--} (double +hyphen). + @item reconfigure Build the home environment described in @var{file}, and switch to it. Switching means that the activation script will be evaluated and (in diff --git a/guix/scripts/home.scm b/guix/scripts/home.scm index 837fd96361..b422cd36e2 100644 --- a/guix/scripts/home.scm +++ b/guix/scripts/home.scm @@ -23,8 +23,21 @@ (define-module (guix scripts home) #:use-module (gnu packages admin) #:use-module ((gnu services) #:hide (delete)) #:use-module (gnu packages) + #:autoload (gnu packages base) (coreutils) + #:autoload (gnu packages bash) (bash) + #:autoload (gnu packages gnupg) (guile-gcrypt) + #:autoload (gnu packages shells) (fish gash zsh) #:use-module (gnu home) #:use-module (gnu home services) + #:autoload (guix modules) (source-module-closure) + #:autoload (gnu build linux-container) (call-with-container %namespaces) + #:autoload (gnu system linux-container) (eval/container) + #:autoload (gnu system file-systems) (file-system-mapping + file-system-mapping-source + file-system-mapping->bind-mount + specification->file-system-mapping + %network-file-mappings) + #:autoload (guix self) (make-config.scm) #:use-module (guix channels) #:use-module (guix derivations) #:use-module (guix ui) @@ -48,6 +61,7 @@ (define-module (guix scripts home) #:use-module (srfi srfi-26) #:use-module (srfi srfi-35) #:use-module (srfi srfi-37) + #:use-module (srfi srfi-71) #:use-module (ice-9 match) #:export (guix-home)) @@ -95,6 +109,16 @@ (define (show-help) (display (G_ " --allow-downgrades for 'reconfigure', allow downgrades to earlier channel revisions")) + (newline) + (display (G_ " + -N, --network allow containers to access the network")) + (display (G_ " + --share=SPEC for containers, share writable host file system + according to SPEC")) + (display (G_ " + --expose=SPEC for containers, expose read-only host file system + according to SPEC")) + (newline) (display (G_ " -v, --verbosity=LEVEL use the given verbosity LEVEL")) (newline) @@ -136,6 +160,22 @@ (define %options (alist-cons 'validate-reconfigure warn-about-backward-reconfigure result))) + + ;; Container options. + (option '(#\N "network") #f #f + (lambda (opt name arg result) + (alist-cons 'network? #t result))) + (option '("share") #t #f + (lambda (opt name arg result) + (alist-cons 'file-system-mapping + (specification->file-system-mapping arg #t) + result))) + (option '("expose") #t #f + (lambda (opt name arg result) + (alist-cons 'file-system-mapping + (specification->file-system-mapping arg #f) + result))) + %standard-build-options)) (define %default-options @@ -149,6 +189,146 @@ (define %default-options (debug . 0) (validate-reconfigure . ,ensure-forward-reconfigure))) + +;;; +;;; Container. +;;; + +(define not-config? + ;; Select (guix …) and (gnu …) modules, except (guix config). + (match-lambda + (('guix 'config) #f) + (('guix _ ...) #t) + (('gnu _ ...) #t) + (_ #f))) + +(define (user-shell) + (match (and=> (or (getenv "SHELL") + (passwd:shell (getpwuid (getuid)))) + basename) + ("zsh" (file-append zsh "/bin/zsh")) + ("fish" (file-append fish "/bin/fish")) + ("gash" (file-append gash "/bin/gash")) + (_ (file-append bash "/bin/bash")))) + +(define %default-system-profile + ;; The "system" profile available when running 'guix home container'. The + ;; activation script currently expects to run "env -0" (XXX), so provide + ;; Coreutils by default. + (delay (profile + (name "home-system-profile") + (content (packages->manifest (list coreutils)))))) + +(define* (spawn-home-container home + #:key + network? + (command '()) + (mappings '()) + (system-profile + (force %default-system-profile))) + "Spawn a login shell within a container running HOME, a home environment. +When COMMAND is a non-empty list, execute it in the container and exit +immediately. Return the exit status of the process in the container." + (define passwd (getpwuid (getuid))) + (define home-directory (or (getenv "HOME") (passwd:dir passwd))) + (define host (gethostname)) + (define uid 1000) + (define gid 1000) + (define user-name (passwd:name passwd)) + (define user-real-name (passwd:gecos passwd)) + + (define (optional-mapping mapping) + (and (file-exists? (file-system-mapping-source mapping)) + mapping)) + + (define network-mappings + (if network? + (filter-map optional-mapping %network-file-mappings) + '())) + + (eval/container + (with-extensions (list guile-gcrypt) + (with-imported-modules `(((guix config) => ,(make-config.scm)) + ,@(source-module-closure + '((gnu build accounts) + (guix profiles) + (guix build utils) + (guix build syscalls)) + #:select? not-config?)) + #~(begin + (use-modules (guix build utils) + (gnu build accounts) + ((guix build syscalls) + #:select (set-network-interface-up))) + + (define shell + #$(user-shell)) + + (define term + #$(getenv "TERM")) + + (define passwd + (password-entry + (name #$user-name) + (real-name #$user-real-name) + (uid #$uid) (gid #$gid) (shell shell) + (directory #$home-directory))) + + (define groups + (list (group-entry (name "users") (gid #$gid)) + (group-entry (gid 65534) ;the overflow GID + (name "overflow")))) + + ;; (guix profiles) loads (guix utils), which calls 'getpw' from the + ;; top level. Thus, arrange so that it's loaded after /etc/passwd + ;; has been created. + (module-autoload! (current-module) + '(guix profiles) '(load-profile)) + + ;; Create /etc/passwd for applications that need it, such as mcron. + (mkdir-p "/etc") + (write-passwd (list passwd)) + (write-group groups) + + (unless #$network? + ;; When isolated from the network, provide a minimal /etc/hosts + ;; to resolve "localhost". + (call-with-output-file "/etc/hosts" + (lambda (port) + (display "127.0.0.1 localhost\n" port) + (chmod port #o444)))) + + ;; Set PATH for things that the activation script might expect, such + ;; as "env". + (load-profile #$system-profile) + + (mkdir-p #$home-directory) + (setenv "HOME" #$home-directory) + (setenv "GUIX_NEW_HOME" #$home) + (primitive-load (string-append #$home "/activate")) + (setenv "GUIX_NEW_HOME" #f) + + (when term + ;; Preserve TERM for proper interactive use. + (setenv "TERM" term)) + + (chdir #$home-directory) + + ;; Invoke SHELL with argv[0] starting with "-": that's how shells + ;; figure out that they are login shells! + (execl shell (string-append "-" (basename shell)) + #$@(match command + (() #~()) + ((_ ...) + #~("-c" #$(string-join command)))))))) + + #:namespaces (if network? + (delq 'net %namespaces) ; share host network + %namespaces) + #:mappings (append network-mappings mappings) + #:guest-uid uid + #:guest-gid gid)) + ;;; ;;; Actions. @@ -159,7 +339,12 @@ (define* (perform-action action he dry-run? derivations-only? use-substitutes? - (validate-reconfigure ensure-forward-reconfigure)) + (validate-reconfigure ensure-forward-reconfigure) + + ;; Container options. + (file-system-mappings '()) + (container-command '()) + network?) "Perform ACTION for home environment. " (define println @@ -180,24 +365,37 @@ (define println (he-out-path -> (derivation->output-path he-drv))) (if (or dry-run? derivations-only?) (return #f) - (begin - (for-each (compose println derivation->output-path) drvs) + (case action + ((reconfigure) + (let* ((number (generation-number %guix-home)) + (generation (generation-file-name + %guix-home (+ 1 number)))) - (case action - ((reconfigure) - (let* ((number (generation-number %guix-home)) - (generation (generation-file-name - %guix-home (+ 1 number)))) - - (switch-symlinks generation he-out-path) - (switch-symlinks %guix-home generation) - (setenv "GUIX_NEW_HOME" he-out-path) - (primitive-load (string-append he-out-path "/activate")) - (setenv "GUIX_NEW_HOME" #f) - (return he-out-path))) - (else - (newline) - (return he-out-path))))))) + (switch-symlinks generation he-out-path) + (switch-symlinks %guix-home generation) + (setenv "GUIX_NEW_HOME" he-out-path) + (primitive-load (string-append he-out-path "/activate")) + (setenv "GUIX_NEW_HOME" #f) + (return he-out-path))) + ((container) + (mlet %store-monad ((status (spawn-home-container + he + #:network? network? + #:mappings file-system-mappings + #:command + container-command))) + (match (status:exit-val status) + (0 (return #t)) + ((? integer? n) (return (exit n))) + (#f + (if (status:term-sig status) + (leave (G_ "process terminated with signal ~a~%") + (status:term-sig status)) + (leave (G_ "process stopped with signal ~a~%") + (status:stop-sig status))))))) + (else + (for-each (compose println derivation->output-path) drvs) + (return he-out-path)))))) (define (process-action action args opts) "Process ACTION, a sub-command, with the arguments are listed in ARGS. @@ -236,6 +434,10 @@ (define (ensure-home-environment file-or-exp obj) (else (leave (G_ "no configuration specified~%"))))))) + (mappings (filter-map (match-lambda + (('file-system-mapping . mapping) mapping) + (_ #f)) + opts)) (dry? (assoc-ref opts 'dry-run?))) (with-store store @@ -256,7 +458,11 @@ (define (ensure-home-environment file-or-exp obj) #:derivations-only? (assoc-ref opts 'derivations-only?) #:use-substitutes? (assoc-ref opts 'substitutes?) #:validate-reconfigure - (assoc-ref opts 'validate-reconfigure)))))) + (assoc-ref opts 'validate-reconfigure) + #:network? (assoc-ref opts 'network?) + #:file-system-mappings mappings + #:container-command + (or (assoc-ref opts 'container-command) '())))))) (warn-about-disk-space))) @@ -345,7 +551,7 @@ (define (parse-sub-command arg result) list-generations describe delete-generations roll-back switch-generation search - import) + import container) (alist-cons 'action action result)) (else (leave (G_ "~a: unknown action~%") action)))))) @@ -383,11 +589,28 @@ (define (fail) (fail)))) args)) + (define (parse-args args) + ;; Parse the list of command line arguments ARGS. + + ;; The '--' token is used to separate the command to run from the rest of + ;; the operands. + (let* ((args rest (break (cut string=? "--" <>) args)) + (opts (parse-command-line args %options (list %default-options) + #:argument-handler + parse-sub-command))) + (match rest + (() opts) + (("--") opts) + (("--" command ...) + (match (assoc-ref opts 'action) + ('container + (alist-cons 'container-command command opts)) + (_ + (leave (G_ "~a: extraneous command~%") + (string-join command)))))))) + (with-error-handling - (let* ((opts (parse-command-line args %options - (list %default-options) - #:argument-handler - parse-sub-command)) + (let* ((opts (parse-args args)) (args (option-arguments opts)) (command (assoc-ref opts 'action))) (parameterize ((%graft? (assoc-ref opts 'graft?))) diff --git a/tests/guix-home.sh b/tests/guix-home.sh index f054d15172..13c02d6269 100644 --- a/tests/guix-home.sh +++ b/tests/guix-home.sh @@ -26,6 +26,16 @@ set -e guix home --version +container_supported () +{ + if guile -c '((@ (guix scripts environment) assert-container-features))' + then + return 0 + else + return 1 + fi +} + 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" @@ -47,20 +57,6 @@ trap 'chmod -Rf +w "$test_directory"; rm -rf "$test_directory"' EXIT ( cd "$test_directory" || exit 77 - HOME="$test_directory" - export HOME - - # - # Test 'guix home reconfigure'. - # - - echo "# This file will be overridden and backed up." > "$HOME/.bashrc" - mkdir "$HOME/.config" - echo "This file will be overridden too." > "$HOME/.config/test.conf" - echo "This file will stay around." > "$HOME/.config/random-file" - - echo -n "# dot-bashrc test file for guix home" > "dot-bashrc" - cat > "home.scm" <<'EOF' (use-modules (guix gexp) (gnu home) @@ -93,6 +89,40 @@ trap 'chmod -Rf +w "$test_directory"; rm -rf "$test_directory"' EXIT "# the content of bashrc-test-config.sh")))))))) EOF + echo -n "# dot-bashrc test file for guix home" > "dot-bashrc" + + + if container_supported + then + # Run the home in a container. + guix home container home.scm -- true + ! guix home container home.scm -- false + test "$(guix home container home.scm -- echo '$HOME')" = "$HOME" + guix home container home.scm -- cat '~/.config/test.conf' | \ + grep "the content of" + guix home container home.scm -- test -h '~/.bashrc' + test "$(guix home container home.scm -- id -u)" = 1000 + ! guix home container home.scm -- test -f '$HOME/sample/home.scm' + guix home container home.scm --expose="$PWD=$HOME/sample" -- \ + test -f '$HOME/sample/home.scm' + ! guix home container home.scm --expose="$PWD=$HOME/sample" -- \ + rm -v '$HOME/sample/home.scm' + else + echo "'guix home container' test SKIPPED" >&2 + fi + + HOME="$test_directory" + export HOME + + # + # Test 'guix home reconfigure'. + # + + echo "# This file will be overridden and backed up." > "$HOME/.bashrc" + mkdir "$HOME/.config" + echo "This file will be overridden too." > "$HOME/.config/test.conf" + echo "This file will stay around." > "$HOME/.config/random-file" + guix home reconfigure "${test_directory}/home.scm" test -d "${HOME}/.guix-home" test -h "${HOME}/.bash_profile"