Skip to content

Extract host config materialization into a reusable Home Manager module#1

Merged
thrix merged 1 commit into
mainfrom
host-config-module
Mar 15, 2026
Merged

Extract host config materialization into a reusable Home Manager module#1
thrix merged 1 commit into
mainfrom
host-config-module

Conversation

@thrix
Copy link
Copy Markdown
Owner

@thrix thrix commented Mar 14, 2026

Summary

  • Replaces inline restoreNixLinks / createHostConfig activation scripts in home.nix with a new modules/host-config.nix Home Manager module
  • Shell logic extracted into modules/host-config-lib.sh — single source of truth for both activation scripts and bats tests
  • Adds hostConfig.{enable, files, xdgDesktopEntries} options with xdgDesktopEntries auto-deriving paths from config.xdg.desktopEntries
  • Uses atomic copy-before-move pattern (cp to .new, mv symlink to .lnk, mv .new to target) for safer materialization
  • Exposes homeManagerModules.hostConfig as a flake output for external consumers
  • Adds bats test suite, GitHub Actions CI (nix build, bats, pre-commit), Dependabot for GHA updates, and make check targets

Assisted-by: Claude Code

Summary by Sourcery

Extract host configuration materialization into a reusable Home Manager module and wire it into the flake for local and external use.

New Features:

  • Introduce a reusable hostConfig Home Manager module that materializes selected symlinked config files as real files for host access.
  • Expose homeManagerModules.hostConfig as a flake output for consumption by other flakes.
  • Add hostConfig options to configure materialized files and optionally include xdg.desktopEntries-derived desktop files.

Enhancements:

  • Refactor inline activation scripts in home.nix into a shared Nix module and shell helper library for restoring and materializing config files.
  • Use a safer, atomic copy-before-move pattern when converting symlinked files into real files.

CI:

  • Add a GitHub Actions workflow to run pre-commit hooks, Nix builds, and bats tests on pushes and pull requests.

Documentation:

  • Document the hostConfig Home Manager module and its flake integration in the README.

Tests:

  • Add a bats test suite covering the host-config shell helper behavior and edge cases.
  • Add Makefile check targets to run Nix builds and bats tests locally.

Chores:

  • Add Dependabot configuration to keep GitHub Actions dependencies up to date.

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Mar 14, 2026

Reviewer's Guide

Extracts host configuration materialization logic into a reusable Home Manager module with shared shell helpers, adds automated tests and CI checks, and exposes the module as a flake output for external use.

Sequence diagram for hostConfig activation and file materialization

sequenceDiagram
  participant HM as HomeManager
  participant Activation as home_activation_scripts
  participant Lib as host_config_lib_sh
  participant FS as Filesystem

  HM->>Activation: run restoreNixLinks (entryBefore checkLinkTargets)
  Activation->>Lib: source host_config_lib_sh
  loop for each file in allFiles
    Activation->>Lib: _hc_restore_file($HOME/file)
    Lib->>FS: rm -f file.new
    alt link_backup_exists
      Lib->>FS: mv file.lnk to file
    end
  end

  HM->>Activation: run createHostConfig (entryAfter linkGeneration)
  Activation->>Lib: source host_config_lib_sh
  loop for each file in allFiles
    Activation->>Lib: _hc_create_file($HOME/file)
    alt file_is_symlink
      Lib->>FS: readlink -f file
      Lib->>FS: cp target to file.new
      Lib->>FS: mv file to file.lnk
      Lib->>FS: mv file.new to file
      Lib->>FS: chmod 644 file
    else not_a_symlink
      Lib-->>Activation: return 0
    end
  end
Loading

Class diagram for the new hostConfig Home Manager module and shell helpers

classDiagram
  class HostConfigModule {
    +bool enable
    +list~string~ files
    +bool xdgDesktopEntries
    +list~string~ desktopFiles
    +list~string~ allFiles
    +string restoreScript
    +string createScript
  }

  class HomeManagerConfig {
    +HostConfigModule hostConfig
    +XdgConfig xdg
    +HomeActivation home_activation
  }

  class XdgConfig {
    +map desktopEntries
  }

  class HomeActivation {
    +string restoreNixLinks
    +string createHostConfig
  }

  class HostConfigLib {
    +_hc_restore_file(path)
    +_hc_create_file(path)
  }

  class FlakeOutputs {
    +HostConfigModule homeManagerModules_hostConfig
  }

  HomeManagerConfig --> HostConfigModule : has
  HomeManagerConfig --> XdgConfig : uses
  HomeManagerConfig --> HomeActivation : configures
  HostConfigModule ..> HostConfigLib : activation_scripts_source
  HostConfigModule --> HomeActivation : populates
  XdgConfig --> HostConfigModule : derives_desktopFiles
  FlakeOutputs --> HostConfigModule : exposes_as_output
Loading

Class diagram for CI workflow and Makefile check targets

classDiagram
  class Makefile {
    +target check
    +target check_nix
    +target check_bats
  }

  class CheckNixTarget {
    +command nix_build_homeConfigurations_thrix_activationPackage
    +command nix_build_homeConfigurations_mvadkert_activationPackage
  }

  class CheckBatsTarget {
    +command bats_tests
  }

  class GithubWorkflowCheck {
    +job pre_commit
    +job nix_build
    +job bats
  }

  class PreCommitJob {
    +uses actions_checkout_v4
    +uses cachix_install_nix_action_v31
    +uses pre_commit_action_v3_0_1
  }

  class NixBuildJob {
    +uses actions_checkout_v4
    +uses cachix_install_nix_action_v31
    +run make_check_nix
  }

  class BatsJob {
    +uses actions_checkout_v4
    +uses cachix_install_nix_action_v31
    +run nix_shell_bats_make_check_bats
  }

  Makefile --> CheckNixTarget : defines
  Makefile --> CheckBatsTarget : defines

  GithubWorkflowCheck --> PreCommitJob : has
  GithubWorkflowCheck --> NixBuildJob : has
  GithubWorkflowCheck --> BatsJob : has

  NixBuildJob --> CheckNixTarget : invokes
  BatsJob --> CheckBatsTarget : invokes
Loading

File-Level Changes

Change Details Files
Extract host config activation logic from home.nix into a dedicated Home Manager module with configurable options.
  • Remove inline restore/create activation script bodies from the main home.nix and replace them with a hostConfig option set
  • Introduce hostConfig.{enable,files,xdgDesktopEntries} configuration that controls which files are materialized and whether desktop entries are auto-included
  • Wire the new host-config module into existing home configurations to ensure it runs as part of activation
home.nix
modules/host-config.nix
flake.nix
Add shared shell helper library implementing safer atomic copy/restore behavior for host materialization and cover it with bats tests.
  • Implement _hc_create_file and _hc_restore_file helpers that use a .new/.lnk atomic pattern and preserve/restore symlinks safely
  • Source the shell helper library from the Nix activation scripts instead of inlining shell logic
  • Create bats tests that exercise the helper functions’ happy path, idempotency, failure behavior, and restore semantics using a temporary fake HOME and store
modules/host-config-lib.sh
tests/host-config.bats
modules/host-config.nix
Expose the host-config module and improve project automation via Makefile targets, CI workflow, and Dependabot.
  • Expose homeManagerModules.hostConfig as a flake output for reuse by external flakes
  • Add make check targets to build home configurations and run bats tests
  • Introduce a GitHub Actions workflow that runs pre-commit, nix builds, and bats tests on pushes/PRs
  • Configure Dependabot to keep GitHub Actions dependencies up to date
  • Document the new module and its usage in README
flake.nix
Makefile
.github/workflows/check.yml
.github/dependabot.yml
README.md

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 2 issues, and left some high level feedback:

  • In modules/host-config-lib.sh, _hc_create_file unconditionally sets mode 644, which will strip execute or group/owner bits if you ever materialize non-config files; consider either preserving the original target’s permissions (e.g. via stat/chmod --reference) or making the mode configurable via a module option.
  • In flake.nix, you both export homeManagerModules.hostConfig and also reference ./modules/host-config.nix directly in the modules list; consider consuming the exported module (e.g. self.homeManagerModules.hostConfig) in your own flake as well to avoid divergence if the module path or wiring changes later.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `modules/host-config-lib.sh`, `_hc_create_file` unconditionally sets mode `644`, which will strip execute or group/owner bits if you ever materialize non-config files; consider either preserving the original target’s permissions (e.g. via `stat`/`chmod --reference`) or making the mode configurable via a module option.
- In `flake.nix`, you both export `homeManagerModules.hostConfig` and also reference `./modules/host-config.nix` directly in the `modules` list; consider consuming the exported module (e.g. `self.homeManagerModules.hostConfig`) in your own flake as well to avoid divergence if the module path or wiring changes later.

## Individual Comments

### Comment 1
<location path="Makefile" line_range="24" />
<code_context>
+	nix build .#homeConfigurations.thrix.activationPackage --no-link
+	nix build .#homeConfigurations.mvadkert.activationPackage --no-link
+
+check/bats:  ## Run bats unit tests
+	bats tests/
+
</code_context>
<issue_to_address>
**suggestion (testing):** Make `check/bats` self-contained by ensuring `bats` is available, similar to CI

This target currently assumes `bats` is installed locally, while CI runs it via `nix shell nixpkgs#bats`. To keep local runs consistent with CI, consider wrapping the command the same way:

```make
check/bats:  ## Run bats unit tests
	nix shell nixpkgs#bats --command bats tests/
```

Alternatively, add a check that fails with a clear error if `bats` is not available on PATH.
</issue_to_address>

### Comment 2
<location path="modules/host-config-lib.sh" line_range="14-23" />
<code_context>
+  fi
+}
+
+_hc_create_file() {
+  local _hc_file="$1"
+  [ -L "$_hc_file" ] || return 0
+  local _hc_target
+  _hc_target=$(readlink -f "$_hc_file")
+  echo -e "\e[32mCopying '$_hc_target' to '$_hc_file'\e[0m"
+  cp "$_hc_target" "$_hc_file.new" || return 1
+  mv "$_hc_file" "$_hc_file.lnk"
+  mv "$_hc_file.new" "$_hc_file"
+  chmod 644 "$_hc_file"
+}
</code_context>
<issue_to_address>
**🚨 suggestion (security):** Consider preserving original file mode instead of forcing permissions to 644

The final `chmod 644 "$_hc_file"` makes every materialized file world‑readable, regardless of the original target’s permissions, which can unintentionally expose sensitive data.

You can instead mirror the target’s mode, falling back to 644 on error:

```sh
_hc_mode=$(stat -c '%a' "$_hc_target") || _hc_mode=644
chmod "$_hc_mode" "$_hc_file"
```

This keeps permissions aligned with the source while retaining a safe default.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread Makefile
Comment thread modules/host-config-lib.sh
Replace inline activation scripts in home.nix with a new
modules/host-config.nix module that materializes Home Manager symlinks
as real files for Silverblue host access.

Changes:
- Extract shell logic into modules/host-config-lib.sh (single source
  of truth for both activation scripts and bats tests)
- Add hostConfig.{enable,files,xdgDesktopEntries} options
- Expose module as homeManagerModules.hostConfig flake output
- Auto-derive desktop entry paths from xdg.desktopEntries (picks up
  dropbox which was previously missing)
- Use atomic copy-before-move pattern for safer file materialization
- Add bats test suite exercising the real shell library
- Add GitHub Actions CI (nix build, bats, pre-commit)
- Add Dependabot for GitHub Actions version updates
- Add make check/nix, check/bats, check targets

Assisted-by: Claude Code
Signed-off-by: Miroslav Vadkerti <mvadkert@redhat.com>
@thrix thrix force-pushed the host-config-module branch from 0ac5525 to f50ee95 Compare March 15, 2026 11:07
@thrix thrix merged commit ee57352 into main Mar 15, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant