From b39e16ec3c937f3c86426c232da8fa846453878f Mon Sep 17 00:00:00 2001 From: technofab Date: Mon, 5 Jan 2026 21:39:41 +0100 Subject: [PATCH 1/4] feat!: add cli with update and list subcommands + gitignore param --- .gitlab-ci.yml | 4 +- .gitlab/renovate.json5 | 2 +- docs/index.md | 1 - lib/module.nix | 256 ++++++++++++++++++++++++++++------------- nix/repo/soonix.nix | 2 +- tests/soonix_test.nix | 20 ++++ 6 files changed, 201 insertions(+), 84 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index a415272..7d70f67 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,5 +1,5 @@ # Generated by soonix, DO NOT EDIT include: -- component: gitlab.com/TECHNOFAB/nix-gitlab-ci/nix-gitlab-ci@3.0.0-alpha.2 +- component: gitlab.com/TECHNOFAB/nix-gitlab-ci/nix-gitlab-ci@3.1.2 inputs: - version: 3.0.0-alpha.2 + version: 3.1.2 diff --git a/.gitlab/renovate.json5 b/.gitlab/renovate.json5 index 88197fc..174045d 100644 --- a/.gitlab/renovate.json5 +++ b/.gitlab/renovate.json5 @@ -16,7 +16,7 @@ }, "postUpgradeTasks": { "commands": [ - "nix-portable nix run .#soonix:update" + "nix-portable nix run .#soonix -- update --skip-gitignore" ] } } diff --git a/docs/index.md b/docs/index.md index 986e3aa..b457a50 100644 --- a/docs/index.md +++ b/docs/index.md @@ -102,4 +102,3 @@ Soonix is designed as a cleaner, more maintainable alternative to Nixago: Ready to start using Soonix? Check out the [Usage Guide](./usage.md) for detailed setup instructions and examples, or browse the [Integration Guide](./integrations.md) to see how to use Soonix with different development tools and frameworks. - diff --git a/lib/module.nix b/lib/module.nix index 4546319..f1c9942 100644 --- a/lib/module.nix +++ b/lib/module.nix @@ -146,90 +146,100 @@ in { Packages for updating the hooks without a devshell. (readonly) ''; example = { - "soonix:update" = ""; + "soonix" = ""; }; }; }; config = let hooks = config.hooks; - hookNames = builtins.attrNames hooks; + # allow excluding gitignore since stuff like renovate can't use/commit it anyways + getHookNames = {includeGitignored ? true}: + if includeGitignored + then (builtins.attrNames hooks) + else + map + (hook: hook.name) + (builtins.filter (hook: !hook.hook.gitignore) (builtins.attrValues hooks)); - runHooks = concatMapStringsSep "\n" (hookName: let - hook = hooks.${hookName}; - modes = { - link = - # sh - '' - if [[ ! -L "${hook.output}" ]] || [[ "$(readlink "${hook.output}")" != "${hook.generatedDerivation}" ]]; then - _soonix_log "info" "${hookName}" "Creating symlink: ${hook.output} -> ${hook.generatedDerivation}" - mkdir -p "$(dirname "${hook.output}")" - ln -sf "${hook.generatedDerivation}" "${hook.output}" - _changed=true - else - _soonix_log "info" "${hookName}" "Symlink up to date: ${hook.output}" - fi - ''; - copy = - # sh - '' - if [[ ! -f "${hook.output}" ]] || ! cmp -s "${hook.generatedDerivation}" "${hook.output}"; then - _soonix_log "info" "${hookName}" "Copying file: ${hook.generatedDerivation} -> ${hook.output}" - mkdir -p "$(dirname "${hook.output}")" - # required since they're read only - rm -f "${hook.output}" - cp "${hook.generatedDerivation}" "${hook.output}" - _changed=true - else - _soonix_log "info" "${hookName}" "File up to date: ${hook.output}" - fi - ''; - }; + runHooks = args: + concatMapStringsSep "\n" (hookName: let + hook = hooks.${hookName}; + modes = { + link = + # sh + '' + if [[ ! -L "${hook.output}" ]] || [[ "$(readlink "${hook.output}")" != "${hook.generatedDerivation}" ]]; then + _soonix_log "info" "${hookName}" "Creating symlink: ${hook.output} -> ${hook.generatedDerivation}" + mkdir -p "$(dirname "${hook.output}")" + ln -sf "${hook.generatedDerivation}" "${hook.output}" + _changed=true + else + _soonix_log "info" "${hookName}" "Symlink up to date: ${hook.output}" + fi + ''; + copy = + # sh + '' + if [[ ! -f "${hook.output}" ]] || ! cmp -s "${hook.generatedDerivation}" "${hook.output}"; then + _soonix_log "info" "${hookName}" "Copying file: ${hook.generatedDerivation} -> ${hook.output}" + mkdir -p "$(dirname "${hook.output}")" + # required since they're read only + rm -f "${hook.output}" + cp "${hook.generatedDerivation}" "${hook.output}" + _changed=true + else + _soonix_log "info" "${hookName}" "File up to date: ${hook.output}" + fi + ''; + }; - optionalGitignore = - if hook.hook.gitignore - then '' - _soonix_add_to_gitignore "${hook.output}" + optionalGitignore = + if hook.hook.gitignore + then '' + _soonix_add_to_gitignore "${hook.output}" + '' + else ""; + in + builtins.addErrorContext "[soonix] while generating script for ${hookName}" + # sh '' - else ""; - in - # sh - '' - # Process hook: ${hookName} - while IFS= read -r line; do - case "$line" in - UPDATED) _soonix_updated+=("${hookName}") ;; - UPTODATE) _soonix_uptodate+=("${hookName}") ;; - *) echo "$line" ;; - esac - done < <( - set -euo pipefail - _changed=false + # Process hook: ${hookName} + while IFS= read -r line; do + case "$line" in + UPDATED) _soonix_updated+=("${hookName}") ;; + UPTODATE) _soonix_uptodate+=("${hookName}") ;; + *) echo "$line" ;; + esac + done < <( + set -euo pipefail + _changed=false - ${modes.${hook.hook.mode} or (throw "Mode ${hook.hook.mode} doesnt exist")} + ${modes.${hook.hook.mode} or (throw "Mode ${hook.hook.mode} doesnt exist")} - # Add to gitignore if requested - ${optionalGitignore} + # Add to gitignore if requested + ${optionalGitignore} - # Run extra commands if file changed - if [[ "$_changed" == "true" && -n "${hook.hook.extra}" ]]; then - _soonix_log "info" "${hookName}" "Running extra command: ${hook.hook.extra}" - eval "${hook.hook.extra}" - fi + # Run extra commands if file changed + if [[ "$_changed" == "true" && -n "${hook.hook.extra}" ]]; then + _soonix_log "info" "${hookName}" "Running extra command: ${hook.hook.extra}" + eval "${hook.hook.extra}" + fi - if [[ "$_changed" == "true" ]]; then - echo "UPDATED" - else - echo "UPTODATE" - fi - ) || { - _soonix_log "error" "${hookName}" "Failed to process hook" - _soonix_failed+=("${hookName}") - } - '') - hookNames; + if [[ "$_changed" == "true" ]]; then + echo "UPDATED" + else + echo "UPTODATE" + fi + ) || { + _soonix_log "error" "${hookName}" "Failed to process hook" + _soonix_failed+=("${hookName}") + } + '') + (getHookNames args); - generatedShellHook = + generateShellHook = args: + builtins.addErrorContext "[soonix] while generating shell hook" # sh '' _soonix_log() { @@ -265,7 +275,7 @@ in { _soonix_failed=() _soonix_uptodate=() - ${runHooks} + ${runHooks args} local output=$'\E[msoonix:\E[38;5;8m' local status=0 @@ -297,8 +307,8 @@ in { in rec { # nothing to do if no hooks exist shellHook = - if (builtins.length hookNames > 0) - then generatedShellHook + if (builtins.length (builtins.attrNames config.hooks) > 0) + then generateShellHook {} else ""; shellHookFile = pkgs.writeShellScript "shellHook" shellHook; devshellModule = { @@ -307,11 +317,99 @@ in { }; finalFiles = buildAllFiles allFiles; # make it simpler to update the hooks without any devshell - packages."soonix:update" = pkgs.writeShellScriptBin "soonix:update" '' - function _soonix() { - ${shellHook} - } - _soonix - ''; + packages = { + soonix = + pkgs.writeShellScriptBin "soonix" + # sh + '' + set -euo pipefail + + SKIP_GITIGNORE=false + COMMAND="" + + show_help() { + cat << EOF + soonix - Declarative file management tool + + USAGE: + soonix [OPTIONS] + + COMMANDS: + update Update all managed files + list List all managed file targets + help Show this help message + + OPTIONS: + --skip-gitignore Skip files that would be added to .gitignore + + EXAMPLES: + soonix update # Update all files + soonix update --skip-gitignore # Update only non-gitignored files + soonix list # List all targets + soonix list --skip-gitignore # List only non-gitignored targets + EOF + } + + while [[ $# -gt 0 ]]; do + case "$1" in + update|list|help) + COMMAND="$1" + shift + ;; + --skip-gitignore) + SKIP_GITIGNORE=true + shift + ;; + *) + echo "Error: Unknown argument '$1'" >&2 + echo "" + show_help + exit 1 + ;; + esac + done + + if [[ -z "$COMMAND" ]]; then + echo "Error: No command specified" >&2 + echo "" + show_help + exit 1 + fi + + case "$COMMAND" in + help) + show_help + ;; + list) + ${concatMapStringsSep "\n" (hookName: let + hook = hooks.${hookName}; + in '' + if [[ "$SKIP_GITIGNORE" == "true" && "${ + if hook.hook.gitignore + then "true" + else "false" + }" == "true" ]]; then + : # skip + else + echo "${hook.output}" + fi + '') (builtins.attrNames hooks)} + ;; + update) + if [[ "$SKIP_GITIGNORE" == "true" ]]; then + function _soonix() { + ${generateShellHook {includeGitignored = false;}} + } + _soonix + else + function _soonix() { + ${generateShellHook {}} + } + _soonix + fi + ;; + esac + ''; + }; }; } diff --git a/nix/repo/soonix.nix b/nix/repo/soonix.nix index fa17fd8..8ca5f8c 100644 --- a/nix/repo/soonix.nix +++ b/nix/repo/soonix.nix @@ -14,7 +14,7 @@ in data = { extends = ["config:recommended"]; postUpgradeTasks.commands = [ - "nix-portable nix run .#soonix:update" + "nix-portable nix run .#soonix -- update --skip-gitignore" ]; lockFileMaintenance = { enabled = true; diff --git a/tests/soonix_test.nix b/tests/soonix_test.nix index e59498e..e5d266a 100644 --- a/tests/soonix_test.nix +++ b/tests/soonix_test.nix @@ -69,6 +69,26 @@ in { assert_file_contains ${shellHook} "gomplate" ''; } + { + name = "packages"; + type = "script"; + script = let + conf = (soonix.make {inherit hooks;}).config; + soonixBin = conf.packages.soonix + "/bin/soonix"; + in + # sh + '' + ${ntlib.helpers.path [pkgs.gnugrep]} + ${ntlib.helpers.scriptHelpers} + + assert -f "${soonixBin}" "should exist" + + assert_file_contains "${soonixBin}" "gotmpl" + assert_file_contains "${soonixBin}" "test.json" + + assert_file_contains "${soonixBin}" "SKIP_GITIGNORE" + ''; + } ]; }; } From 374e31d8521535360eec4f5b0fd028d14fc2a9ea Mon Sep 17 00:00:00 2001 From: technofab Date: Mon, 5 Jan 2026 22:12:36 +0100 Subject: [PATCH 2/4] chore(devShells): use new soonix syntax --- nix/repo/devShells.nix | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/nix/repo/devShells.nix b/nix/repo/devShells.nix index c473189..272f851 100644 --- a/nix/repo/devShells.nix +++ b/nix/repo/devShells.nix @@ -41,8 +41,10 @@ in { } { name = "soonix"; + files = "nix run .#soonix -- list --skip-gitignore"; stage_fixed = true; - run = "nix run .#soonix:update"; + # {files} is needed so lefthook git add's them + run = "nix run .#soonix -- update --skip-gitignore; #{files}"; } ]; }; From 5caa66ec8d82fbf24e4d5b7373da18bc1e2017df Mon Sep 17 00:00:00 2001 From: technofab Date: Tue, 20 Jan 2026 21:52:00 +0100 Subject: [PATCH 3/4] feat(cli): add check mode which exits 2 if hooks/files are outdated --- lib/module.nix | 145 +++++++++++++++++++++++------------------- tests/soonix_test.nix | 1 + 2 files changed, 82 insertions(+), 64 deletions(-) diff --git a/lib/module.nix b/lib/module.nix index f1c9942..ad57a16 100644 --- a/lib/module.nix +++ b/lib/module.nix @@ -154,57 +154,65 @@ in { config = let hooks = config.hooks; # allow excluding gitignore since stuff like renovate can't use/commit it anyways - getHookNames = {includeGitignored ? true}: - if includeGitignored - then (builtins.attrNames hooks) - else - map - (hook: hook.name) - (builtins.filter (hook: !hook.hook.gitignore) (builtins.attrValues hooks)); - - runHooks = args: - concatMapStringsSep "\n" (hookName: let - hook = hooks.${hookName}; - modes = { - link = - # sh - '' - if [[ ! -L "${hook.output}" ]] || [[ "$(readlink "${hook.output}")" != "${hook.generatedDerivation}" ]]; then - _soonix_log "info" "${hookName}" "Creating symlink: ${hook.output} -> ${hook.generatedDerivation}" + runHooks = concatMapStringsSep "\n" (hookName: let + hook = hooks.${hookName}; + modes = { + link = + # sh + '' + if [[ ! -L "${hook.output}" ]] || [[ "$(readlink "${hook.output}")" != "${hook.generatedDerivation}" ]]; then + _soonix_log "info" "${hookName}" "Creating symlink: ${hook.output} -> ${hook.generatedDerivation}" + if [[ "$CHECK_MODE" != "true" ]]; then mkdir -p "$(dirname "${hook.output}")" ln -sf "${hook.generatedDerivation}" "${hook.output}" - _changed=true else - _soonix_log "info" "${hookName}" "Symlink up to date: ${hook.output}" + _soonix_log "info" "${hookName}" "Would create symlink: ${hook.output} -> ${hook.generatedDerivation}" fi - ''; - copy = - # sh - '' - if [[ ! -f "${hook.output}" ]] || ! cmp -s "${hook.generatedDerivation}" "${hook.output}"; then - _soonix_log "info" "${hookName}" "Copying file: ${hook.generatedDerivation} -> ${hook.output}" + _changed=true + else + _soonix_log "info" "${hookName}" "Symlink up to date: ${hook.output}" + fi + ''; + copy = + # sh + '' + if [[ ! -f "${hook.output}" ]] || ! cmp -s "${hook.generatedDerivation}" "${hook.output}"; then + _soonix_log "info" "${hookName}" "Copying file: ${hook.generatedDerivation} -> ${hook.output}" + if [[ "$CHECK_MODE" != "true" ]]; then mkdir -p "$(dirname "${hook.output}")" # required since they're read only rm -f "${hook.output}" cp "${hook.generatedDerivation}" "${hook.output}" - _changed=true else - _soonix_log "info" "${hookName}" "File up to date: ${hook.output}" + _soonix_log "info" "${hookName}" "Would copy file: ${hook.generatedDerivation} -> ${hook.output}" fi - ''; - }; + _changed=true + else + _soonix_log "info" "${hookName}" "File up to date: ${hook.output}" + fi + ''; + }; - optionalGitignore = - if hook.hook.gitignore - then '' - _soonix_add_to_gitignore "${hook.output}" - '' - else ""; - in - builtins.addErrorContext "[soonix] while generating script for ${hookName}" - # sh + optionalGitignore = + if hook.hook.gitignore + then '' + _soonix_add_to_gitignore "${hook.output}" '' - # Process hook: ${hookName} + else ""; + + isGitignored = + if hook.hook.gitignore + then "true" + else "false"; + in + builtins.addErrorContext "[soonix] while generating script for ${hookName}" + # sh + '' + # Process hook: ${hookName} + # Skip if SKIP_GITIGNORE is set and this hook is gitignored + if [[ "$SKIP_GITIGNORE" == "true" && "${isGitignored}" == "true" ]]; then + : # skip + else while IFS= read -r line; do case "$line" in UPDATED) _soonix_updated+=("${hookName}") ;; @@ -217,13 +225,15 @@ in { ${modes.${hook.hook.mode} or (throw "Mode ${hook.hook.mode} doesnt exist")} - # Add to gitignore if requested - ${optionalGitignore} + if [[ "$CHECK_MODE" != "true" ]]; then + # Add to gitignore if requested + ${optionalGitignore} - # Run extra commands if file changed - if [[ "$_changed" == "true" && -n "${hook.hook.extra}" ]]; then - _soonix_log "info" "${hookName}" "Running extra command: ${hook.hook.extra}" - eval "${hook.hook.extra}" + # Run extra commands if file changed + if [[ "$_changed" == "true" && -n "${hook.hook.extra}" ]]; then + _soonix_log "info" "${hookName}" "Running extra command: ${hook.hook.extra}" + eval "${hook.hook.extra}" + fi fi if [[ "$_changed" == "true" ]]; then @@ -235,10 +245,11 @@ in { _soonix_log "error" "${hookName}" "Failed to process hook" _soonix_failed+=("${hookName}") } - '') - (getHookNames args); + fi + '') + (builtins.attrNames hooks); - generateShellHook = args: + generateShellHook = builtins.addErrorContext "[soonix] while generating shell hook" # sh '' @@ -275,13 +286,16 @@ in { _soonix_failed=() _soonix_uptodate=() - ${runHooks args} + ${runHooks} local output=$'\E[msoonix:\E[38;5;8m' local status=0 if [[ ''${#_soonix_updated[@]} -gt 0 ]]; then output="$output [updated: ''${_soonix_updated[*]}]" >&2 + if [[ "$CHECK_MODE" == "true" ]]; then + status=2 + fi fi if [[ ''${#_soonix_uptodate[@]} -gt 0 ]]; then output="$output [unchanged: ''${_soonix_uptodate[*]}]" >&2 @@ -293,8 +307,8 @@ in { printf "%s\E[m\n" "$output" >&2 - if [[ $status -eq 1 ]]; then - exit 1 + if [[ $status -ne 0 ]]; then + exit $status fi ''; @@ -308,7 +322,7 @@ in { # nothing to do if no hooks exist shellHook = if (builtins.length (builtins.attrNames config.hooks) > 0) - then generateShellHook {} + then generateShellHook else ""; shellHookFile = pkgs.writeShellScript "shellHook" shellHook; devshellModule = { @@ -325,6 +339,7 @@ in { set -euo pipefail SKIP_GITIGNORE=false + CHECK_MODE=false COMMAND="" show_help() { @@ -336,6 +351,7 @@ in { COMMANDS: update Update all managed files + check Check if all files are up to date (dry-run) list List all managed file targets help Show this help message @@ -352,7 +368,7 @@ in { while [[ $# -gt 0 ]]; do case "$1" in - update|list|help) + update|check|list|help) COMMAND="$1" shift ;; @@ -396,17 +412,18 @@ in { '') (builtins.attrNames hooks)} ;; update) - if [[ "$SKIP_GITIGNORE" == "true" ]]; then - function _soonix() { - ${generateShellHook {includeGitignored = false;}} - } - _soonix - else - function _soonix() { - ${generateShellHook {}} - } - _soonix - fi + function _soonix() { + ${generateShellHook} + } + _soonix + ;; + check) + CHECK_MODE=true + echo "Checking files..." + function _soonix() { + ${generateShellHook} + } + _soonix ;; esac ''; diff --git a/tests/soonix_test.nix b/tests/soonix_test.nix index e5d266a..39b6bbf 100644 --- a/tests/soonix_test.nix +++ b/tests/soonix_test.nix @@ -87,6 +87,7 @@ in { assert_file_contains "${soonixBin}" "test.json" assert_file_contains "${soonixBin}" "SKIP_GITIGNORE" + assert_file_contains "${soonixBin}" "CHECK_MODE" ''; } ]; From 837ee45bed850378cf246bb90e467577c8a53ef1 Mon Sep 17 00:00:00 2001 From: technofab Date: Wed, 21 Jan 2026 17:08:41 +0100 Subject: [PATCH 4/4] fix(module): set CHECK_MODE and SKIP_GITIGNORE in shell hook to default add mkCLI shorthand --- lib/default.nix | 1 + lib/module.nix | 3 +++ 2 files changed, 4 insertions(+) diff --git a/lib/default.nix b/lib/default.nix index ae29377..0ce2d3f 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -22,4 +22,5 @@ in rec { }; mkShellHook = userConfig: (make userConfig).config.shellHook; + mkCLI = userConfig: (make userConfig).config.packages.soonix; } diff --git a/lib/module.nix b/lib/module.nix index ad57a16..3ea44aa 100644 --- a/lib/module.nix +++ b/lib/module.nix @@ -253,6 +253,9 @@ in { builtins.addErrorContext "[soonix] while generating shell hook" # sh '' + CHECK_MODE=''${CHECK_MODE:-false} + SKIP_GITIGNORE=''${SKIP_GITIGNORE:-false} + _soonix_log() { local level="$1" local hook="$2"