diff --git a/mkosi.conf.d/20-packages-network.conf b/mkosi.conf.d/20-packages-network.conf index 977b883..29bf65c 100644 --- a/mkosi.conf.d/20-packages-network.conf +++ b/mkosi.conf.d/20-packages-network.conf @@ -5,4 +5,5 @@ Packages=bind iproute2 nfs-utils + nss-mdns # Needed for Avahi mDNS for local domain resolving etc. ufw # Simple firewall; consider switching to firewalld once the KCM supports zones diff --git a/mkosi.finalize.d/00-modify_nsswitch.py b/mkosi.finalize.d/00-modify_nsswitch.py new file mode 100644 index 0000000..21da09a --- /dev/null +++ b/mkosi.finalize.d/00-modify_nsswitch.py @@ -0,0 +1,57 @@ +# SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL +# SPDX-FileCopyrightText: 2025 Hadi Chokr + +import os +import sys + +def modify_nsswitch(): + path = "/etc/nsswitch.conf" + + print(f"Checking {path}...") # Debug line + + # Read the file + try: + with open(path, "r") as file: + lines = file.readlines() + except Exception as e: + print(f"Error reading {path}: {e}", file=sys.stderr) + raise # Re-raise the exception + + modified = False + + # Process each line + for i, line in enumerate(lines): + print(f"Checking line: {line.strip()}") # Debug line + if line.startswith("hosts:"): + if "mymachines" in line and "mdns_minimal" not in line: + lines[i] = line.replace("mymachines", "mymachines mdns_minimal [NOTFOUND=return]", 1) + modified = True + print("Added mdns_minimal after mymachines.") # Debug line + elif "mymachines" not in line and "mdns_minimal" not in line: + lines[i] = line.replace("hosts:", "hosts: mdns_minimal [NOTFOUND=return] ", 1) + modified = True + print("Added mdns_minimal.") # Debug line + break + + if not modified: + raise RuntimeError("Expected modification but no changes were made.") + + # Write back the modified file + try: + with open(path, "w") as file: + file.writelines(lines) + print("Updated /etc/nsswitch.conf") + except Exception as e: + print(f"Error writing to {path}: {e}", file=sys.stderr) + raise # Re-raise the exception + +if __name__ == "__main__": + if os.geteuid() != 0: + print("This script must be run as root.", file=sys.stderr) + sys.exit(1) + + try: + modify_nsswitch() + except Exception as e: + print(f"Fatal error: {e}", file=sys.stderr) + sys.exit(1) diff --git a/mkosi.finalize.chroot b/mkosi.finalize.d/mkosi.finalize.chroot similarity index 100% rename from mkosi.finalize.chroot rename to mkosi.finalize.d/mkosi.finalize.chroot