From 5ad5c9bae634fd17ad998cee6a2cf30f45ba5560 Mon Sep 17 00:00:00 2001 From: teutat3s Date: Sun, 20 Nov 2022 23:10:39 +0100 Subject: [PATCH] Switch to alejandra for formatting Use treefmt For context, see upstream PR: https://github.com/divnix/digga/pull/491 --- modules/core/packages.nix | 129 +++++++++++++++++++------------------- overlays/overrides.nix | 28 ++++----- shell/devos.nix | 79 +++++++++++++---------- shell/hooks/pre-commit.sh | 45 +++++-------- treefmt.toml | 22 +++++++ 5 files changed, 164 insertions(+), 139 deletions(-) create mode 100644 treefmt.toml diff --git a/modules/core/packages.nix b/modules/core/packages.nix index f6867816..f0a35d17 100644 --- a/modules/core/packages.nix +++ b/modules/core/packages.nix @@ -1,75 +1,78 @@ -{ config, pkgs, lib, ... }: - -with lib; -let +{ + config, + pkgs, + lib, + ... +}: +with lib; let psCfg = config.pub-solar; cfg = config.pub-solar.core; -in -{ +in { environment = { - systemPackages = with pkgs; [ - # Core unix utility packages - coreutils-full - dnsutils - inetutils - progress - pciutils - usbutils + systemPackages = with pkgs; + [ + # Core unix utility packages + coreutils-full + dnsutils + inetutils + progress + pciutils + usbutils - wget - openssl - openssh - curl - htop - lsof - psmisc - file + wget + openssl + openssh + curl + htop + lsof + psmisc + file - # zippit - zip - unzip + # zippit + zip + unzip - # Modern modern utilities - p7zip - croc - jq - ] + # Modern modern utilities + p7zip + croc + jq + ] + ++ lib.optionals (!cfg.lite) [ + mtr - ++ lib.optionals (!cfg.lite) [ - mtr + gitFull + git-lfs + git-bug - gitFull - git-lfs - git-bug + xdg-utils + sysfsutils + renameutils + nfs-utils + moreutils + mailutils + keyutils + input-utils + elfutils + binutils + dateutils + diffutils + findutils + exfat - xdg-utils - sysfsutils - renameutils - nfs-utils - moreutils - mailutils - keyutils - input-utils - elfutils - binutils - dateutils - diffutils - findutils - exfat + # Nix specific utilities + alejandra + niv + manix + nix-index + nix-tree + nixpkgs-review + # Build broken, python2.7-PyJWT-2.0.1.drv' failed + #nixops + psos + nvd - # Nix specific utilities - niv - manix - nix-index - nix-tree - nixpkgs-review - # Build broken, python2.7-PyJWT-2.0.1.drv' failed - #nixops - psos - nvd - - # Fun - neofetch - ]; + # Fun + neofetch + ]; }; } diff --git a/overlays/overrides.nix b/overlays/overrides.nix index c0521cf0..52f74e4c 100644 --- a/overlays/overrides.nix +++ b/overlays/overrides.nix @@ -1,25 +1,23 @@ channels: final: prev: { - __dontExport = true; # overrides clutter up actual creations - inherit (channels.latest) + inherit + (channels.latest) cachix dhall discord element-desktop rage nix-index - nixpkgs-fmt qutebrowser + alejandra signal-desktop starship deploy-rs nix - tdesktop arduino arduino-cli - gurk-rs thunderbird swaynotificationcenter @@ -32,16 +30,18 @@ channels: final: prev: { #inherit (channels.fork) # ; - haskellPackages = prev.haskellPackages.override + haskellPackages = + prev.haskellPackages.override (old: { - overrides = prev.lib.composeExtensions (old.overrides or (_: _: { })) (hfinal: hprev: - let version = prev.lib.replaceChars [ "." ] [ "" ] prev.ghc.version; - in - { - # same for haskell packages, matching ghc versions - inherit (channels.latest.haskell.packages."ghc${version}") - haskell-language-server; - }); + overrides = prev.lib.composeExtensions (old.overrides or (_: _: {})) (hfinal: hprev: let + version = prev.lib.replaceChars ["."] [""] prev.ghc.version; + in { + # same for haskell packages, matching ghc versions + inherit + (channels.latest.haskell.packages."ghc${version}") + haskell-language-server + ; + }); }); # Example to override node package diff --git a/shell/devos.nix b/shell/devos.nix index 9aefcc6f..3b83163d 100644 --- a/shell/devos.nix +++ b/shell/devos.nix @@ -1,29 +1,40 @@ -{ pkgs, extraModulesPath, inputs, lib, ... }: -let - - inherit (pkgs) +{ + pkgs, + extraModulesPath, + inputs, + lib, + ... +}: let + inherit + (pkgs) agenix cachix editorconfig-checker mdbook nix - nixpkgs-fmt + alejandra nvfetcher + shellcheck + shfmt + treefmt + ; + + inherit + (pkgs.nodePackages) + prettier ; hooks = import ./hooks; - pkgWithCategory = category: package: { inherit package category; }; + pkgWithCategory = category: package: {inherit package category;}; devos = pkgWithCategory "devos"; linter = pkgWithCategory "linter"; docs = pkgWithCategory "docs"; - -in -{ +in { _file = toString ./.; - imports = [ "${extraModulesPath}/git/hooks.nix" ]; - git = { inherit hooks; }; + imports = ["${extraModulesPath}/git/hooks.nix"]; + git = {inherit hooks;}; # override for our own welcome devshell.name = pkgs.lib.mkForce "PubSolarOS"; @@ -43,26 +54,30 @@ in unset _PATH ''); - commands = with pkgs; [ - (devos nix) - (devos agenix) - { - category = "devos"; - name = pkgs.nvfetcher.pname; - help = pkgs.nvfetcher.meta.description; - command = "cd $PRJ_ROOT/pkgs; ${pkgs.nvfetcher}/bin/nvfetcher -c ./sources.toml $@"; - } - (linter nixpkgs-fmt) - (linter editorconfig-checker) + commands = with pkgs; + [ + (devos nix) + (devos agenix) + { + category = "devos"; + name = pkgs.nvfetcher.pname; + help = pkgs.nvfetcher.meta.description; + command = "cd $PRJ_ROOT/pkgs; ${pkgs.nvfetcher}/bin/nvfetcher -c ./sources.toml $@"; + } + (linter alejandra) + (linter editorconfig-checker) + (linter nodePackages.prettier) + (linter shfmt) + (linter shellcheck) + (linter treefmt) - (docs mdbook) - ] - ++ lib.optionals (!pkgs.stdenv.buildPlatform.isi686) [ - (devos cachix) - ] - ++ lib.optionals (pkgs.stdenv.hostPlatform.isLinux && !pkgs.stdenv.buildPlatform.isDarwin) [ - (devos inputs.nixos-generators.defaultPackage.${pkgs.system}) - (devos deploy-rs) - ] - ; + (docs mdbook) + ] + ++ lib.optionals (!pkgs.stdenv.buildPlatform.isi686) [ + (devos cachix) + ] + ++ lib.optionals (pkgs.stdenv.hostPlatform.isLinux && !pkgs.stdenv.buildPlatform.isDarwin) [ + (devos inputs.nixos-generators.defaultPackage.${pkgs.system}) + (devos deploy-rs) + ]; } diff --git a/shell/hooks/pre-commit.sh b/shell/hooks/pre-commit.sh index 4adf6c0a..4ce4fa13 100755 --- a/shell/hooks/pre-commit.sh +++ b/shell/hooks/pre-commit.sh @@ -1,43 +1,28 @@ #!/usr/bin/env bash -if git rev-parse --verify HEAD >/dev/null 2>&1 -then +if git rev-parse --verify HEAD >/dev/null 2>&1; then against=HEAD else # Initial commit: diff against an empty tree object - against=$(git hash-object -t tree /dev/null) + against=$(${git}/bin/git hash-object -t tree /dev/null) fi diff="git diff-index --name-only --cached $against --diff-filter d" -mapfile -t nix_files < <($diff -- '*.nix') -mapfile -t all_files < <($diff) +all_files=($($diff)) -# Format staged nix files -if (( ${#nix_files[@]} != 0 )); then - # Stash only unstaged changes, keeping staged changes - old_stash=$(git rev-parse --quiet --verify refs/stash) - git stash push --quiet --keep-index -m 'Unstaged changes before pre-commit hook' - new_stash=$(git rev-parse --quiet --verify refs/stash) +# Format staged files. +if ((${#all_files[@]} != 0)); then + treefmt "${all_files[@]}" && + git add "${all_files[@]}" +fi - # Format staged changes - nixpkgs-fmt "${nix_files[@]}" \ - && git add "${nix_files[@]}" - - # If unstaged changes were stashed re-apply to working tree - if [ "$old_stash" != "$new_stash" ]; then - git stash pop --quiet +# check editorconfig +if ((${#all_files[@]} != 0)); then + if ! editorconfig-checker -- "${all_files[@]}"; then + printf "%b\n" \ + "\nCode is not aligned with .editorconfig" \ + "Review the output and commit your fixes" >&2 + exit 1 fi fi - -# Check editorconfig -if (( ${#all_files[@]} != 0 )); then - editorconfig-checker -- "${all_files[@]}" -fi - -if [[ $? != '0' ]]; then - printf "%b\n" \ - "\nCode is not aligned with .editorconfig" \ - "Review the output and commit your fixes" >&2 - exit 1 -fi diff --git a/treefmt.toml b/treefmt.toml new file mode 100644 index 00000000..c7c1710d --- /dev/null +++ b/treefmt.toml @@ -0,0 +1,22 @@ +[formatter.nix] +command = "alejandra" +includes = ["*.nix"] + +[formatter.prettier] +command = "prettier" +options = ["--write"] +includes = [ + "*.json", + "*.yaml", + "*.md", +] + +[formatter.shell] +command = "shfmt" +options = [ + "-s", + "-w", + "-i", + "2", +] +includes = ["*.sh"]