From 5d4569ed9c8c07702c02bd8c61459ebcb2d1e595 Mon Sep 17 00:00:00 2001 From: Leonard Sheng Sheng Lee <305414+sheeeng@users.noreply.github.com> Date: Mon, 15 Dec 2025 13:52:05 +0100 Subject: [PATCH] feat(nix): formatters Use https://github.com/numtide/treefmt-nix to format codes. Signed-off-by: Leonard Sheng Sheng Lee <305414+sheeeng@users.noreply.github.com> --- .github/workflows/pr-check-signed-commits.yml | 4 +- .../workflows/update-flake-dependencies.yml | 2 +- flake.lock | 37 ++++++++++- flake.nix | 28 ++++++-- nix/home-manager.nix | 7 +- nix/nixos.nix | 7 +- nix/shell-plugins.nix | 64 +++++++++---------- plugins/circleci/test-fixtures/cli.yaml | 6 +- plugins/gitea/test-fixtures/config.yml | 26 ++++---- plugins/gitea/test-fixtures/import_config.yml | 38 +++++------ plugins/github/test-fixtures/hosts.yml | 12 ++-- .../github/test-fixtures/hosts_gho_token.yml | 6 +- .../glab-config-self-hosted.yaml | 8 +-- plugins/gitlab/test-fixtures/glab-config.yaml | 8 +-- treefmt.nix | 48 ++++++++++++++ 15 files changed, 205 insertions(+), 96 deletions(-) create mode 100644 treefmt.nix diff --git a/.github/workflows/pr-check-signed-commits.yml b/.github/workflows/pr-check-signed-commits.yml index 425472107..f6d809288 100644 --- a/.github/workflows/pr-check-signed-commits.yml +++ b/.github/workflows/pr-check-signed-commits.yml @@ -1,9 +1,9 @@ -name: Check signed commits in PR +name: Check signed commits in PR on: pull_request_target jobs: build: - name: Check signed commits in PR + name: Check signed commits in PR permissions: contents: read pull-requests: write diff --git a/.github/workflows/update-flake-dependencies.yml b/.github/workflows/update-flake-dependencies.yml index 98b94723a..e47237851 100644 --- a/.github/workflows/update-flake-dependencies.yml +++ b/.github/workflows/update-flake-dependencies.yml @@ -17,7 +17,7 @@ jobs: FILE_TO_COMMIT: flake.lock COMMIT_BRANCH: automation/update-flake-dependencies COMMIT_MESSAGE: "chore(nix): Update Flake dependencies" - run: | + run: |- # fetch remote state git fetch # if branch exists on remote already diff --git a/flake.lock b/flake.lock index f15a97b5d..87d513014 100644 --- a/flake.lock +++ b/flake.lock @@ -16,10 +16,27 @@ "type": "github" } }, + "nixpkgs_2": { + "locked": { + "lastModified": 1761236834, + "narHash": "sha256-+pthv6hrL5VLW2UqPdISGuLiUZ6SnAXdd2DdUE+fV2Q=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "d5faa84122bc0a1fd5d378492efce4e289f8eac1", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, "root": { "inputs": { "nixpkgs": "nixpkgs", - "systems": "systems" + "systems": "systems", + "treefmt-nix": "treefmt-nix" } }, "systems": { @@ -36,6 +53,24 @@ "repo": "default", "type": "github" } + }, + "treefmt-nix": { + "inputs": { + "nixpkgs": "nixpkgs_2" + }, + "locked": { + "lastModified": 1762938485, + "narHash": "sha256-AlEObg0syDl+Spi4LsZIBrjw+snSVU4T8MOeuZJUJjM=", + "owner": "numtide", + "repo": "treefmt-nix", + "rev": "5b4ee75aeefd1e2d5a1cc43cf6ba65eba75e83e4", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "treefmt-nix", + "type": "github" + } } }, "root": "root", diff --git a/flake.nix b/flake.nix index 1e93543d7..ad1ddd7c7 100644 --- a/flake.nix +++ b/flake.nix @@ -2,19 +2,35 @@ inputs = { nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; systems.url = "github:nix-systems/default"; + treefmt-nix.url = "github:numtide/treefmt-nix"; }; - outputs = { nixpkgs, systems, ... }: + outputs = + { + nixpkgs, + systems, + treefmt-nix, + ... + }: let - forAllSystems = function: - nixpkgs.lib.genAttrs (import systems) ( - system: function nixpkgs.legacyPackages.${system} - ); + forAllSystems = + function: nixpkgs.lib.genAttrs (import systems) (system: function nixpkgs.legacyPackages.${system}); + + treefmtEval = forAllSystems (pkgs: treefmt-nix.lib.evalModule pkgs ./treefmt.nix); in { + formatter = forAllSystems (pkgs: treefmtEval.${pkgs.system}.config.build.wrapper); + + checks = forAllSystems (pkgs: { + formatting = treefmtEval.${pkgs.system}.config.build.check (pkgs.path or ./.); + }); + devShells = forAllSystems (pkgs: { default = pkgs.mkShell { name = "Shell with Go toolchain"; - packages = with pkgs; [ go gopls ]; + packages = with pkgs; [ + go + gopls + ]; }; }); diff --git a/nix/home-manager.nix b/nix/home-manager.nix index 590a1fa3b..93a8af57a 100644 --- a/nix/home-manager.nix +++ b/nix/home-manager.nix @@ -1,4 +1,9 @@ -{ pkgs, lib, config, ... }: +{ + pkgs, + lib, + config, + ... +}: import ./shell-plugins.nix { inherit pkgs; inherit lib; diff --git a/nix/nixos.nix b/nix/nixos.nix index 7ae12ba3c..ad05a7b01 100644 --- a/nix/nixos.nix +++ b/nix/nixos.nix @@ -1,4 +1,9 @@ -{ pkgs, lib, config, ... }: +{ + pkgs, + lib, + config, + ... +}: import ./shell-plugins.nix { inherit pkgs; inherit lib; diff --git a/nix/shell-plugins.nix b/nix/shell-plugins.nix index 2269814be..b45c852ee 100644 --- a/nix/shell-plugins.nix +++ b/nix/shell-plugins.nix @@ -1,4 +1,10 @@ -{ pkgs, lib, config, is-home-manager, ... }: +{ + pkgs, + lib, + config, + is-home-manager, + ... +}: with lib; let cfg = config.programs._1password-shell-plugins; @@ -44,15 +50,13 @@ in # for which the executable has a supported 1Password Shell Plugin apply = package_list: - map - ( - package: - if (elem (getExeName package) supported_plugins) then - package - else - abort "${getExeName package} is not a valid 1Password Shell Plugin. A list of supported plugins can be found by running `op plugin list` or at: https://developer.1password.com/docs/cli/shell-plugins/" - ) - package_list; + map ( + package: + if (elem (getExeName package) supported_plugins) then + package + else + abort "${getExeName package} is not a valid 1Password Shell Plugin. A list of supported plugins can be found by running `op plugin list` or at: https://developer.1password.com/docs/cli/shell-plugins/" + ) package_list; }; }; }; @@ -80,26 +84,20 @@ in # end # ``` # where `{pkg}` is the executable name of the package - posixFunctions = map - (package: '' - ${package}() { - op plugin run -- ${package} "$@"; - } - '') - pkg-exe-names; - fishFunctions = map - (package: '' - function ${package} --wraps "${package}" --description "1Password Shell Plugin for ${package}" - op plugin run -- ${package} $argv - end - '') - pkg-exe-names; + posixFunctions = map (package: '' + ${package}() { + op plugin run -- ${package} "$@"; + } + '') pkg-exe-names; + fishFunctions = map (package: '' + function ${package} --wraps "${package}" --description "1Password Shell Plugin for ${package}" + op plugin run -- ${package} $argv + end + '') pkg-exe-names; packages = lib.optional (cfg.package != null) cfg.package ++ cfg.plugins; in mkIf cfg.enable (mkMerge [ - { - programs.fish.interactiveShellInit = strings.concatStringsSep "\n" fishFunctions; - } + { programs.fish.interactiveShellInit = strings.concatStringsSep "\n" fishFunctions; } (optionalAttrs is-home-manager { programs = { # for the Bash and Zsh home-manager modules, @@ -109,20 +107,22 @@ in }; home = { inherit packages; - sessionVariables = { OP_PLUGINS_SOURCED = "1"; }; + sessionVariables = { + OP_PLUGINS_SOURCED = "1"; + }; }; }) (optionalAttrs (!is-home-manager) { programs = { - bash.interactiveShellInit = - strings.concatStringsSep "\n" posixFunctions; + bash.interactiveShellInit = strings.concatStringsSep "\n" posixFunctions; zsh.interactiveShellInit = strings.concatStringsSep "\n" posixFunctions; }; environment = { systemPackages = packages; - variables = { OP_PLUGINS_SOURCED = "1"; }; + variables = { + OP_PLUGINS_SOURCED = "1"; + }; }; }) ]); } - diff --git a/plugins/circleci/test-fixtures/cli.yaml b/plugins/circleci/test-fixtures/cli.yaml index 0ae053540..0f5e858e3 100644 --- a/plugins/circleci/test-fixtures/cli.yaml +++ b/plugins/circleci/test-fixtures/cli.yaml @@ -5,6 +5,6 @@ rest_endpoint: api/v2 tls_cert: "" tls_insecure: false orb_publishing: - default_namespace: "" - default_vcs_provider: "" - default_owner: "" \ No newline at end of file + default_namespace: "" + default_vcs_provider: "" + default_owner: "" diff --git a/plugins/gitea/test-fixtures/config.yml b/plugins/gitea/test-fixtures/config.yml index 3aee1b79e..af1ba98a4 100644 --- a/plugins/gitea/test-fixtures/config.yml +++ b/plugins/gitea/test-fixtures/config.yml @@ -1,14 +1,14 @@ logins: -- name: https://git.example.com - url: https://git.example.com - token: oyyfsny27bgphldmhvffxhhlmqvdkzjrfslrsj9f - default: true - ssh_host: "" - ssh_key: "" - insecure: false - ssh_certificate_principal: "" - ssh_agent: false - ssh_key_agent_pub: "" - version_check: false - user: example - created: 0 + - name: https://git.example.com + url: https://git.example.com + token: oyyfsny27bgphldmhvffxhhlmqvdkzjrfslrsj9f + default: true + ssh_host: "" + ssh_key: "" + insecure: false + ssh_certificate_principal: "" + ssh_agent: false + ssh_key_agent_pub: "" + version_check: false + user: example + created: 0 diff --git a/plugins/gitea/test-fixtures/import_config.yml b/plugins/gitea/test-fixtures/import_config.yml index 568b0d4aa..88e55636d 100644 --- a/plugins/gitea/test-fixtures/import_config.yml +++ b/plugins/gitea/test-fixtures/import_config.yml @@ -1,23 +1,23 @@ logins: -- name: git.example.com - url: https://git.example.com - token: oyyfsny27bgphldmhvffxhhlmqvdkzjrfslrsj9f - default: false - ssh_host: git.example.com - ssh_key: ~/.ssh/id_ed25519 - insecure: false - user: example - created: 1479340800000 -- name: gitea.com - url: https://gitea.com - token: enjkarzu2ca5ffcnvzaczxncuczeoq9utlpqqrzs - default: false - ssh_host: git.example.com - ssh_key: ~/.ssh/id_ed25519 - insecure: true - user: example@example.com - created: 1479340800000 + - name: git.example.com + url: https://git.example.com + token: oyyfsny27bgphldmhvffxhhlmqvdkzjrfslrsj9f + default: false + ssh_host: git.example.com + ssh_key: ~/.ssh/id_ed25519 + insecure: false + user: example + created: 1479340800000 + - name: gitea.com + url: https://gitea.com + token: enjkarzu2ca5ffcnvzaczxncuczeoq9utlpqqrzs + default: false + ssh_host: git.example.com + ssh_key: ~/.ssh/id_ed25519 + insecure: true + user: example@example.com + created: 1479340800000 preferences: editor: false flag_defaults: - remote: "" \ No newline at end of file + remote: "" diff --git a/plugins/github/test-fixtures/hosts.yml b/plugins/github/test-fixtures/hosts.yml index 42f6b9221..ee4d3bf54 100644 --- a/plugins/github/test-fixtures/hosts.yml +++ b/plugins/github/test-fixtures/hosts.yml @@ -1,9 +1,9 @@ github.com: - user: testUser - oauth_token: ghp_B2xokoZVNaqxLhWzxXY0MoARZyZlgEXAMPLE - git_protocol: https + user: testUser + oauth_token: ghp_B2xokoZVNaqxLhWzxXY0MoARZyZlgEXAMPLE + git_protocol: https enterprise.github.com: - user: enterpriseUser - oauth_token: ghp_4v7aWhddhdqyIR1RmqiRcUjTgzrNFEXAMPLE - git_protocol: ssh + user: enterpriseUser + oauth_token: ghp_4v7aWhddhdqyIR1RmqiRcUjTgzrNFEXAMPLE + git_protocol: ssh diff --git a/plugins/github/test-fixtures/hosts_gho_token.yml b/plugins/github/test-fixtures/hosts_gho_token.yml index 552306905..718ced471 100644 --- a/plugins/github/test-fixtures/hosts_gho_token.yml +++ b/plugins/github/test-fixtures/hosts_gho_token.yml @@ -1,4 +1,4 @@ github.com: - user: testUser2 - oauth_token: gho_8BQUJmcVkoNo7LqYY0Ng1wndPbwwfEXAMPLE - git_protocol: https + user: testUser2 + oauth_token: gho_8BQUJmcVkoNo7LqYY0Ng1wndPbwwfEXAMPLE + git_protocol: https diff --git a/plugins/gitlab/test-fixtures/glab-config-self-hosted.yaml b/plugins/gitlab/test-fixtures/glab-config-self-hosted.yaml index e65eb7951..f50210a21 100644 --- a/plugins/gitlab/test-fixtures/glab-config-self-hosted.yaml +++ b/plugins/gitlab/test-fixtures/glab-config-self-hosted.yaml @@ -2,7 +2,7 @@ git_protocol: ssh check_update: false display_hyperlinks: false hosts: - gitlab.acme.com: - api_protocol: https - api_host: api.gitlab.acme.com - token: glpat-sJy3L26ZNW7A3EXAMPLE + gitlab.acme.com: + api_protocol: https + api_host: api.gitlab.acme.com + token: glpat-sJy3L26ZNW7A3EXAMPLE diff --git a/plugins/gitlab/test-fixtures/glab-config.yaml b/plugins/gitlab/test-fixtures/glab-config.yaml index 095786556..0c4d84b59 100644 --- a/plugins/gitlab/test-fixtures/glab-config.yaml +++ b/plugins/gitlab/test-fixtures/glab-config.yaml @@ -2,7 +2,7 @@ git_protocol: ssh check_update: false display_hyperlinks: false hosts: - gitlab.com: - api_protocol: https - api_host: gitlab.com - token: glpat-sJy3L26ZNW7A3EXAMPLE + gitlab.com: + api_protocol: https + api_host: gitlab.com + token: glpat-sJy3L26ZNW7A3EXAMPLE diff --git a/treefmt.nix b/treefmt.nix new file mode 100644 index 000000000..e0fe2a2d4 --- /dev/null +++ b/treefmt.nix @@ -0,0 +1,48 @@ +{ pkgs, ... }: +{ + projectRootFile = "flake.nix"; + + programs = { + # keep-sorted start block=yes + deadnix.enable = true; + keep-sorted.enable = true; + nixfmt = { + enable = true; + package = pkgs.nixfmt-rfc-style; + includes = [ "*.nix" ]; + strict = true; + }; + statix.enable = true; + yamlfmt = { + enable = true; + settings = { + formatter = { + eof_newline = true; + max_line_length = -1; + retain_line_breaks_single = true; + scan_folded_as_literal = true; + trim_trailing_whitespace = true; + }; + }; + }; + # keep-sorted end + }; + + settings = { + global.excludes = [ + "*.lock" + "*.patch" + "LICENSE*" + ".github/**/*.md" + ]; + }; + + settings.formatter = { + keep-sorted = { + options = [ + "--mode" + "fix" + ]; + }; + }; +}