Skip to content

Commit

Permalink
image: update mkosi and use package directory feature
Browse files Browse the repository at this point in the history
  • Loading branch information
malt3 committed Feb 16, 2024
1 parent c69722c commit 37e34bc
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 54 deletions.
26 changes: 12 additions & 14 deletions bazel/mkosi/mkosi_image.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
""" Bazel rule for building mkosi images. """

load("@aspect_bazel_lib//lib:paths.bzl", "relative_file")
load("@bazel_skylib//lib:paths.bzl", "paths")

def _mkosi_image_impl(ctx):
Expand All @@ -9,7 +8,6 @@ def _mkosi_image_impl(ctx):
outputs = []
tools = []
workdir = ctx.file.mkosi_conf.dirname
config_rel = lambda target: relative_file(target, ctx.file.mkosi_conf.path)
env = {}
args.add("-C", workdir)
if ctx.attr.distribution:
Expand All @@ -19,24 +17,24 @@ def _mkosi_image_impl(ctx):
if ctx.attr.output:
args.add("--output", ctx.attr.output)
args.add_all(ctx.attr.packages, before_each = "--package")
for package_file in ctx.files.package_files:
args.add("--package", config_rel(package_file.path))
for package_dir in ctx.files.package_directories:
args.add("--package-directory", package_dir.path)
if len(ctx.files.local_mirror) > 0:
env["LOCAL_MIRROR"] = config_rel(ctx.files.local_mirror[0].dirname)
env["LOCAL_MIRROR"] = ctx.files.local_mirror[0].dirname
for tree in ctx.files.base_trees:
args.add("--base-tree", config_rel(tree.path))
args.add("--base-tree", tree.path)
for tree in ctx.files.skeleton_trees:
args.add("--skeleton-tree", config_rel(tree.path))
args.add("--skeleton-tree", tree.path)
for tree in ctx.files.package_manager_trees:
args.add("--package-manager_tree", config_rel(tree.path))
args.add("--package-manager-tree", tree.path)
for tree in ctx.files.extra_trees:
args.add("--extra-tree", config_rel(tree.path))
args.add("--extra-tree", tree.path)
for initrd in ctx.files.initrds:
inputs.append(initrd)
args.add("--initrd", config_rel(initrd.path))
args.add("--initrd", initrd.path)
inputs.extend(ctx.files.mkosi_conf)
inputs.extend(ctx.files.srcs[:])
inputs.extend(ctx.files.package_files[:])
inputs.extend(ctx.files.package_directories[:])
inputs.extend(ctx.files.base_trees[:])
inputs.extend(ctx.files.skeleton_trees[:])
inputs.extend(ctx.files.package_manager_trees[:])
Expand All @@ -59,9 +57,9 @@ def _mkosi_image_impl(ctx):
if output.is_directory and out_dir.path.startswith(output.path + "/"):
fail("output directory {} is nested within output directory {}; outputs cannot be nested within each other!".format(out_dir.path, output.path))
outputs.append(out_dir)
args.add("--output-dir", config_rel(out_dir.path))
args.add("--output-dir", out_dir.path)
else:
args.add("--output-dir", config_rel(paths.join(ctx.bin_dir.path, ctx.label.package)))
args.add("--output-dir", paths.join(ctx.bin_dir.path, ctx.label.package))
args.add_all(ctx.attr.extra_args)
for key, value in ctx.attr.env.items():
args.add("--environment", "{}={}".format(key, value))
Expand Down Expand Up @@ -126,7 +124,7 @@ mkosi_image = rule(
"out_dir": attr.string(),
"output": attr.string(),
"outs": attr.output_list(),
"package_files": attr.label_list(allow_files = True),
"package_directories": attr.label_list(allow_files = True),
"package_manager_trees": attr.label_list(allow_files = True),
"packages": attr.string_list(),
"seed": attr.string(),
Expand Down
31 changes: 23 additions & 8 deletions bazel/mkosi/mkosi_wrapper.sh.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,24 @@ shopt -s inherit_errexit

export PATH=/run/wrappers/bin:/run/current-system/sw/bin:/bin:/usr/bin:/usr/local/bin
VERSION_ARG=""
args=("$@")
args=()

while [[ $# -gt 0 ]]; do
key="$1"
case $key in
--*-tree|--initrd|--package-directory|--output-dir)
# absolutize any file paths
shift # past the key and to the value
value="$1"
args+=("${key}" "$(realpath "${value}")")
shift # past the value
;;
*)
args+=("$1")
shift
;;
esac
done

if [[ -n ${VERSION_FILE+x} ]]; then
VERSION_ARG="--environment=IMAGE_VERSION=$(cat "${VERSION_FILE}")"
Expand All @@ -14,14 +31,12 @@ fi
if [[ -n ${LOCAL_MIRROR+x} ]]; then
LOCAL_MIRROR=$(realpath "${LOCAL_MIRROR}")
reposdir=$(mktemp -d)
cat > "${reposdir}/mkosi.repo" << EOF
[local-mirror]
name=local-mirror
baseurl=file://${LOCAL_MIRROR}
enabled=1
gpgcheck=0
EOF
# putting an empty repo file under /etc/yum.repos.d/mkosi.repo
# will make mkosi use only package directories
# and not try to fetch packages from the network
touch "${reposdir}/mkosi.repo"
args+=("--package-manager-tree=${reposdir}:/etc/yum.repos.d")
args+=("--package-directory" "${LOCAL_MIRROR}")
fi

exec @@MKOSI@@ "${args[@]}" build
12 changes: 5 additions & 7 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,11 @@
callPackage = pkgsUnstable.callPackage;

mkosiDev = (pkgsUnstable.mkosi.overrideAttrs (oldAttrs: rec {
# TODO(malt3): remove patch once merged and released upstream (systemd/mkosi#2163)
src = pkgsUnstable.fetchFromGitHub {
owner = "systemd";
repo = "mkosi";
rev = "abf22cdc6ccb13f2cd84679ede77231455ec6813";
hash = "sha256-njtYWSXSLMcn6AtGfAeL/ncZQ6g+Vgpe7EaKLkzAOl4=";
};
propagatedBuildInputs = oldAttrs.propagatedBuildInputs ++ (with pkgsUnstable; [
# package management
dnf5
rpm
createrepo_c

# filesystem tools
squashfsTools # mksquashfs
Expand All @@ -48,6 +42,10 @@
cpio # cpio
zstd # zstd
xz # xz

# utils
gnused # sed
gnugrep # grep
]);
}));

Expand Down
27 changes: 2 additions & 25 deletions image/base/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
load("@aspect_bazel_lib//lib:copy_file.bzl", "copy_file")
load("@aspect_bazel_lib//lib:copy_to_directory.bzl", "copy_to_directory")
load("@rules_pkg//:pkg.bzl", "pkg_tar")
load("//bazel/mkosi:mkosi_image.bzl", "mkosi_image")
Expand Down Expand Up @@ -27,25 +26,6 @@ copy_to_directory(
replace_prefixes = {"file": ""},
)

[
copy_file(
name = name,
src = "@" + name + "//file",
out = name + ".rpm",
allow_symlink = True,
)
for name in [
"kernel_lts",
"kernel_core_lts",
"kernel_modules_lts",
"kernel_modules_core_lts",
"kernel_mainline",
"kernel_core_mainline",
"kernel_modules_mainline",
"kernel_modules_core_mainline",
]
]

[
mkosi_image(
name = "base_" + kernel_variant,
Expand All @@ -71,11 +51,8 @@ copy_to_directory(
local_mirror = ["@mkosi_rpms//:repo"],
mkosi_conf = "mkosi.conf",
output = kernel_variant,
package_files = [
":kernel_" + kernel_variant,
":kernel_core_" + kernel_variant,
":kernel_modules_" + kernel_variant,
":kernel_modules_core_" + kernel_variant,
package_directories = [
":rpms_" + kernel_variant,
],
tags = [
"manual",
Expand Down
3 changes: 3 additions & 0 deletions image/base/mkosi.conf
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ Packages=systemd
dbus
udev
util-linux
kernel
kernel-core
kernel-modules

# nvme / disk / udev tools
Packages=nvme-cli
Expand Down

0 comments on commit 37e34bc

Please sign in to comment.