]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Add mkosi init to install a tmpfiles dropin for the cache dir 3618/head
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Tue, 25 Mar 2025 15:17:39 +0000 (16:17 +0100)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Tue, 25 Mar 2025 19:10:46 +0000 (20:10 +0100)
Let's clean up packages after they haven't been touched for 90
days.

Note that this has to be explicitly enabled by users by running mkosi
init for now. In the future we can look into automatically enabling
this.

mkosi/__init__.py
mkosi/config.py
mkosi/resources/man/mkosi.1.md
mkosi/resources/tmpfiles.d/mkosi.conf [new file with mode: 0644]
mkosi/user.py
pyproject.toml

index 602ee2e2f01a5d165f71574a0578f87f4899f9de..b0dc3a38bb9008270223f03520f93b6c8263aa0b 100644 (file)
@@ -139,7 +139,7 @@ from mkosi.sandbox import (
 )
 from mkosi.sysupdate import run_sysupdate
 from mkosi.tree import copy_tree, make_tree, move_tree, rmtree
-from mkosi.user import become_root_cmd
+from mkosi.user import INVOKING_USER, become_root_cmd
 from mkosi.util import (
     PathString,
     current_home_dir,
@@ -4931,6 +4931,11 @@ def run_build(
 def run_verb(args: Args, images: Sequence[Config], *, resources: Path) -> None:
     images = list(images)
 
+    if args.verb == Verb.init:
+        copy_tree(resources / "tmpfiles.d", INVOKING_USER.tmpfiles_dir(), preserve=False)
+        log_notice(f"Copied mkosi tmpfiles dropins to {INVOKING_USER.tmpfiles_dir()}")
+        return
+
     if args.verb == Verb.completion:
         return print_completion(args, resources=resources)
 
index 59ae35cbf1b2b9a11026a6c7f0bc1c3e959b5d46..cffb687be1b1029bb876aa83394732a48f4917e7 100644 (file)
@@ -81,6 +81,7 @@ class Verb(StrEnum):
     completion = enum.auto()
     sysupdate = enum.auto()
     sandbox = enum.auto()
+    init = enum.auto()
 
     def supports_cmdline(self) -> bool:
         return self in (
@@ -119,6 +120,7 @@ class Verb(StrEnum):
             Verb.documentation,
             Verb.dependencies,
             Verb.completion,
+            Verb.init,
         )
 
 
@@ -4153,7 +4155,8 @@ def create_argument_parser(chdir: bool = True) -> argparse.ArgumentParser:
         # the synopsis below is supposed to be indented by two spaces
         usage="\n  "
         + textwrap.dedent("""\
-              mkosi [options…] {b}summary{e}
+              mkosi [options…] {b}init{e}
+                mkosi [options…] {b}summary{e}
                 mkosi [options…] {b}cat-config{e}
                 mkosi [options…] {b}build{e}         [-- command line…]
                 mkosi [options…] {b}shell{e}         [-- command line…]
index 4d78b375ac9039a9dd796ef53ea92413e74dedae..c5c6a71d900b8a291d7a1fe258e7e667ee6178b6 100644 (file)
@@ -8,6 +8,8 @@ mkosi — Build Bespoke OS Images
 
 # SYNOPSIS
 
+`mkosi [options…] init`
+
 `mkosi [options…] summary`
 
 `mkosi [options…] cat-config`
@@ -57,6 +59,13 @@ mkosi — Build Bespoke OS Images
 
 The following command line verbs are known:
 
+`init`
+:   Initialize **mkosi**. This is a one time operation that sets up various
+    config files required for an optimal experience. Currently this only
+    initialized a `tmpfiles.d` dropin for the mkosi package cache
+    directory to make sure old, unused files are cleaned up
+    automatically.
+
 `summary`
 :   Show a human-readable summary of all options used for building the images.
     This will parse the command line and configuration files, but only print
diff --git a/mkosi/resources/tmpfiles.d/mkosi.conf b/mkosi/resources/tmpfiles.d/mkosi.conf
new file mode 100644 (file)
index 0000000..fdb74f7
--- /dev/null
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: LGPL-2.1-or-later
+
+d %C/mkosi - - - 90d
index b4ca80a537723ba960ed89ba137603f492aa35aa..78e5f05dbec77bbbe9d0cf3bf3795c2b8d988ad3 100644 (file)
@@ -40,6 +40,14 @@ class INVOKING_USER:
 
         return d
 
+    @classmethod
+    def tmpfiles_dir(cls) -> Path:
+        config = Path(os.getenv("XDG_CONFIG_HOME", Path.home() / ".config"))
+        if config in (Path("/"), Path("/root")):
+            return Path("/etc/tmpfiles.d")
+
+        return config / "user-tmpfiles.d"
+
     @classmethod
     def chown(cls, path: Path) -> None:
         # If we created a file/directory in a parent directory owned by a regular user, make sure the path
index 2aa36685b1e97ad3af06ff07f675f763aac624cb..92a9fc88cea6837b6abd7ef5f9b70eb542dc75d6 100644 (file)
@@ -42,6 +42,7 @@ packages = [
     "mkosi-tools/**/*",
     "mkosi-vm/**/*",
     "repart/**/*",
+    "tmpfiles.d/*",
 ]
 
 [tool.isort]