Skip to content

Instantly share code, notes, and snippets.

@Mic92
Created October 16, 2024 16:43
Show Gist options
  • Save Mic92/e214d88c0a8f5f3fd2babd7b467be5b3 to your computer and use it in GitHub Desktop.
Save Mic92/e214d88c0a8f5f3fd2babd7b467be5b3 to your computer and use it in GitHub Desktop.
diff --git a/nixos/modules/system/etc/build-composefs-dump.py b/nixos/modules/system/etc/build-composefs-dump.py
index fe739a621ec4..42287ea35842 100644
--- a/nixos/modules/system/etc/build-composefs-dump.py
+++ b/nixos/modules/system/etc/build-composefs-dump.py
@@ -1,5 +1,3 @@
-#!/usr/bin/env python3
-
"""Build a composefs dump from a Json config
See the man page of composefs-dump for details about the format:
@@ -57,8 +55,9 @@ def __init__(
path: str | None = None,
):
if path is None:
- path = attrs["target"]
- self.path = path
+ self.path = attrs["target"]
+ else:
+ self.path = path
self.size = size
self.filetype = filetype
self.mode = mode
@@ -176,14 +175,25 @@ def main() -> None:
add_leading_directories(glob_target, attrs, paths)
else: # Without globbing
if mode == "symlink" or mode == "direct-symlink":
- composefs_path = ComposefsPath(
- attrs,
- # A high approximation of the size of a symlink
- size=100,
- filetype=FileType.symlink,
- mode="0777",
- payload=source,
- )
+ if text := attrs.get("text"):
+ Path(target).write_text(text)
+ composefs_path = ComposefsPath(
+ attrs,
+ # A high approximation of the size of a symlink
+ size=100,
+ filetype=FileType.file,
+ mode="0777",
+ payload=text,
+ )
+ else:
+ composefs_path = ComposefsPath(
+ attrs,
+ # A high approximation of the size of a symlink
+ size=100,
+ filetype=FileType.symlink,
+ mode="0777",
+ payload=source,
+ )
elif os.path.isdir(source):
composefs_path = ComposefsPath(
attrs,
diff --git a/nixos/modules/system/etc/etc.nix b/nixos/modules/system/etc/etc.nix
index 594b9aab61b7..1189d7b357fb 100644
--- a/nixos/modules/system/etc/etc.nix
+++ b/nixos/modules/system/etc/etc.nix
@@ -303,20 +303,25 @@ in
]) etcHardlinks}
'';
- system.build.etcMetadataImage =
- let
- etcJson = pkgs.writeText "etc-json" (builtins.toJSON etc');
- etcDump = pkgs.runCommand "etc-dump" { } ''
- ${lib.getExe pkgs.buildPackages.python3} ${./build-composefs-dump.py} ${etcJson} > $out
- '';
- in
- pkgs.runCommand "etc-metadata.erofs" {
- nativeBuildInputs = with pkgs.buildPackages; [ composefs erofs-utils ];
- } ''
- mkcomposefs --from-file ${etcDump} $out
- fsck.erofs $out
- '';
-
+ system.build.etcMetadataImage = pkgs.runCommand "etc-metadata.erofs" {
+ etcJson = builtins.toJSON (builtins.map (attr: {
+ inherit (attr) target mode uid gid;
+ # avoid create text derivations if we can
+ source = if attr.text == null then attr.source else attr.text;
+ text = attr.text;
+ }) etc');
+ passAsFile = [ "etcJson" ];
+ nativeBuildInputs = with pkgs.buildPackages; [
+ (writers.writePython3Bin "build-composefs-dump" {} (builtins.readFile ./build-composefs-dump.py))
+ composefs
+ erofs-utils
+ ];
+ } ''
+ build-composefs-dump "$etcJsonPath" > etc-dump
+ cat etc-dump
+ mkcomposefs --from-file etc-dump $out
+ fsck.erofs $out
+ '';
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment