diff --git a/nix/module.nix b/nix/module.nix index e225439..4a92ad4 100644 --- a/nix/module.nix +++ b/nix/module.nix @@ -29,8 +29,29 @@ in boot.kernel.sysctl."kernel.apparmor_restrict_unprivileged_userns" = lib.mkDefault 0; system.activationScripts.amber-pm-state = lib.mkIf cfg.initializeState '' - if [ ! -e /var/lib/apm/apm ]; then + target="/var/lib/apm/apm" + version_file="$target/.amber-pm-version" + current_version="${cfg.package.version}" + + if [ ! -e "$target" ]; then + echo "APM state directory not found, initializing..." ${cfg.package}/bin/amber-pm-init-state + echo "Running ace-init for first-time setup..." + /var/lib/apm/apm/files/bin/ace-init + elif [ -f "$version_file" ]; then + stored_version="$(cat "$version_file")" + if [ "$stored_version" != "$current_version" ]; then + echo "APM version changed ($stored_version -> $current_version), re-initializing..." + ${cfg.package}/bin/amber-pm-init-state --force + echo "Running ace-init..." + /var/lib/apm/apm/files/bin/ace-init + else + echo "APM version unchanged ($current_version), skipping ace-init." + fi + else + echo "No version file found, refreshing state and running ace-init..." + ${cfg.package}/bin/amber-pm-init-state --force + /var/lib/apm/apm/files/bin/ace-init fi ''; }; diff --git a/nix/package.nix b/nix/package.nix index f8ebadd..42ce4cc 100644 --- a/nix/package.nix +++ b/nix/package.nix @@ -174,13 +174,15 @@ fi mkdir -p "$target" cp -a "$seed"/. "$target"/ +echo '@version@' > "$target/.amber-pm-version" chmod -R u+rwX "$target" echo "Initialized $target" echo "Next step: run '/var/lib/apm/apm/files/bin/ace-init' as root, or run 'apm --help' for CLI smoke testing." EOF substituteInPlace "$out/bin/amber-pm-init-state" \ --replace-fail '@bash@' '${bash}' \ - --replace-fail '@out@' "$out" + --replace-fail '@out@' "$out" \ + --replace-fail '@version@' '${version}' chmod +x "$out/bin/amber-pm-init-state" runHook postInstall