From 0cf16893eb91a7af98ba8e91264bb92fa34cc9ab Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Tue, 25 Mar 2025 16:17:39 +0100 Subject: [PATCH] Add mkosi init to install a tmpfiles dropin for the cache dir 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 | 7 ++++++- mkosi/config.py | 5 ++++- mkosi/resources/man/mkosi.1.md | 9 +++++++++ mkosi/resources/tmpfiles.d/mkosi.conf | 3 +++ mkosi/user.py | 8 ++++++++ pyproject.toml | 1 + 6 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 mkosi/resources/tmpfiles.d/mkosi.conf diff --git a/mkosi/__init__.py b/mkosi/__init__.py index 602ee2e2f..b0dc3a38b 100644 --- a/mkosi/__init__.py +++ b/mkosi/__init__.py @@ -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) diff --git a/mkosi/config.py b/mkosi/config.py index 59ae35cbf..cffb687be 100644 --- a/mkosi/config.py +++ b/mkosi/config.py @@ -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…] diff --git a/mkosi/resources/man/mkosi.1.md b/mkosi/resources/man/mkosi.1.md index 4d78b375a..c5c6a71d9 100644 --- a/mkosi/resources/man/mkosi.1.md +++ b/mkosi/resources/man/mkosi.1.md @@ -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 index 000000000..fdb74f7aa --- /dev/null +++ b/mkosi/resources/tmpfiles.d/mkosi.conf @@ -0,0 +1,3 @@ +# SPDX-License-Identifier: LGPL-2.1-or-later + +d %C/mkosi - - - 90d diff --git a/mkosi/user.py b/mkosi/user.py index b4ca80a53..78e5f05db 100644 --- a/mkosi/user.py +++ b/mkosi/user.py @@ -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 diff --git a/pyproject.toml b/pyproject.toml index 2aa36685b..92a9fc88c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -42,6 +42,7 @@ packages = [ "mkosi-tools/**/*", "mkosi-vm/**/*", "repart/**/*", + "tmpfiles.d/*", ] [tool.isort] -- 2.47.2