formatting: add flake apps + prepare hooks

This commit is contained in:
DavHau 2022-03-07 13:57:22 +07:00
parent 058bc6cf9b
commit 68478d0772
5 changed files with 208 additions and 29 deletions

2
.gitignore vendored
View File

@ -1,4 +1,6 @@
.*/ .*/
!.github/ !.github/
/.pre-commit-config.yaml
result result
interpreter interpreter

View File

@ -1,5 +1,26 @@
{ {
"nodes": { "nodes": {
"alejandra": {
"inputs": {
"flakeCompat": "flakeCompat",
"nixpkgs": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1646360966,
"narHash": "sha256-fJ/WHSU45bMJRDqz9yA3B2lwXtW5DKooU+Pzn13GyZI=",
"owner": "kamadorueda",
"repo": "alejandra",
"rev": "511c3f6a88b6964e1496fb6f441f4ae5e58bd3ea",
"type": "github"
},
"original": {
"owner": "kamadorueda",
"repo": "alejandra",
"type": "github"
}
},
"crane": { "crane": {
"flake": false, "flake": false,
"locked": { "locked": {
@ -16,6 +37,37 @@
"type": "github" "type": "github"
} }
}, },
"flake-utils-pre-commit": {
"locked": {
"lastModified": 1644229661,
"narHash": "sha256-1YdnJAsNy69bpcjuoKdOYQX0YxZBiCYZo4Twxerqv7k=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "3cecb5b042f7f209c56ffd8371b2711a290ec797",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"flakeCompat": {
"flake": false,
"locked": {
"lastModified": 1641205782,
"narHash": "sha256-4jY7RCWUoZ9cKD8co0/4tFARpWB+57+r1bLLvXNJliY=",
"owner": "edolstra",
"repo": "flake-compat",
"rev": "b7547d3eed6f32d06102ead8991ec52ab0a4f1a7",
"type": "github"
},
"original": {
"owner": "edolstra",
"repo": "flake-compat",
"type": "github"
}
},
"gomod2nix": { "gomod2nix": {
"flake": false, "flake": false,
"locked": { "locked": {
@ -95,14 +147,40 @@
"type": "github" "type": "github"
} }
}, },
"pre-commit-hooks": {
"inputs": {
"flake-utils": [
"flake-utils-pre-commit"
],
"nixpkgs": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1646153636,
"narHash": "sha256-AlWHMzK+xJ1mG267FdT8dCq/HvLCA6jwmx2ZUy5O8tY=",
"owner": "cachix",
"repo": "pre-commit-hooks.nix",
"rev": "b6bc0b21e1617e2b07d8205e7fae7224036dfa4b",
"type": "github"
},
"original": {
"owner": "cachix",
"repo": "pre-commit-hooks.nix",
"type": "github"
}
},
"root": { "root": {
"inputs": { "inputs": {
"alejandra": "alejandra",
"crane": "crane", "crane": "crane",
"flake-utils-pre-commit": "flake-utils-pre-commit",
"gomod2nix": "gomod2nix", "gomod2nix": "gomod2nix",
"mach-nix": "mach-nix", "mach-nix": "mach-nix",
"nixpkgs": "nixpkgs", "nixpkgs": "nixpkgs",
"node2nix": "node2nix", "node2nix": "node2nix",
"poetry2nix": "poetry2nix" "poetry2nix": "poetry2nix",
"pre-commit-hooks": "pre-commit-hooks"
} }
} }
}, },

149
flake.nix
View File

@ -4,6 +4,18 @@
inputs = { inputs = {
nixpkgs.url = "nixpkgs/nixos-unstable"; nixpkgs.url = "nixpkgs/nixos-unstable";
### dev dependencies
alejandra.url = github:kamadorueda/alejandra;
alejandra.inputs.nixpkgs.follows = "nixpkgs";
pre-commit-hooks.url = "github:cachix/pre-commit-hooks.nix";
pre-commit-hooks.inputs.nixpkgs.follows = "nixpkgs";
# upstream flake-utils dep not supporting `aarch64-darwin` yet
flake-utils-pre-commit.url = "github:numtide/flake-utils";
pre-commit-hooks.inputs.flake-utils.follows = "flake-utils-pre-commit";
### framework dependencies
# required for builder go/gomod2nix # required for builder go/gomod2nix
gomod2nix = { url = "github:tweag/gomod2nix"; flake = false; }; gomod2nix = { url = "github:tweag/gomod2nix"; flake = false; };
@ -22,16 +34,20 @@
outputs = { outputs = {
self, self,
alejandra,
gomod2nix, gomod2nix,
mach-nix, mach-nix,
nixpkgs, nixpkgs,
node2nix, node2nix,
poetry2nix, poetry2nix,
pre-commit-hooks,
crane, crane,
...
}@inp: }@inp:
let let
b = builtins; b = builtins;
l = lib // builtins;
lib = nixpkgs.lib; lib = nixpkgs.lib;
@ -40,9 +56,10 @@
supportedSystems = [ "x86_64-linux" "x86_64-darwin" "aarch64-darwin" ]; supportedSystems = [ "x86_64-linux" "x86_64-darwin" "aarch64-darwin" ];
forAllSystems = f: lib.genAttrs supportedSystems (system: forSystems = systems: f: lib.genAttrs systems
f system nixpkgs.legacyPackages.${system} (system: f system nixpkgs.legacyPackages.${system});
);
forAllSystems = forSystems supportedSystems;
# To use dream2nix in non-flake + non-IFD enabled repos, the source code of dream2nix # To use dream2nix in non-flake + non-IFD enabled repos, the source code of dream2nix
# must be installed into these repos (using nix run dream2nix#install). # must be installed into these repos (using nix run dream2nix#install).
@ -111,6 +128,24 @@
}; };
}); });
pre-commit-check = forAllSystems (system: pkgs:
pre-commit-hooks.lib.${system}.run {
src = ./.;
hooks = {
treefmt = {
enable = true;
name = "treefmt";
pass_filenames = true;
entry = l.toString (pkgs.writeScript "treefmt" ''
#!${pkgs.bash}/bin/bash
export PATH="$PATH:${alejandra.defaultPackage.${system}}/bin"
${pkgs.treefmt}/bin/treefmt --fail-on-change "$@"
'');
};
};
}
);
in in
{ {
# System independent dream2nix api. # System independent dream2nix api.
@ -139,11 +174,41 @@
tests-impure.type = "app"; tests-impure.type = "app";
tests-impure.program = b.toString tests-impure.program = b.toString
(dream2nixFor."${system}".callPackageDream ./tests/impure {}); (dream2nixFor."${system}".callPackageDream ./tests/impure {});
tests-unit.type = "app"; tests-unit.type = "app";
tests-unit.program = b.toString tests-unit.program = b.toString
(dream2nixFor."${system}".callPackageDream ./tests/unit { (dream2nixFor."${system}".callPackageDream ./tests/unit {
inherit self; inherit self;
}); });
tests-all.type = "app";
tests-all.program = l.toString
(dream2nixFor.${system}.utils.writePureShellScript
[
alejandra.defaultPackage.${system}
pkgs.coreutils
pkgs.gitMinimal
pkgs.nix
]
''
echo "running unit tests"
${self.apps.${system}.tests-unit.program}
echo "running impure CLI tests"
${self.apps.${system}.tests-impure.program}
echo "running nix flake check"
cd $WORKDIR
nix flake check
'');
# passes through extra flags to treefmt
format.type = "app";
format.program = l.toString
(pkgs.writeScript "format" ''
export PATH="${alejandra.defaultPackage.${system}}/bin"
${pkgs.treefmt}/bin/treefmt "$@"
'');
} }
); );
@ -151,37 +216,67 @@
# use via 'nix develop . -c $SHELL' # use via 'nix develop . -c $SHELL'
devShell = forAllSystems (system: pkgs: pkgs.mkShell { devShell = forAllSystems (system: pkgs: pkgs.mkShell {
buildInputs = with pkgs; buildInputs =
(with pkgs; [ (with pkgs; [
nixUnstable nix
treefmt
]) ])
++ [
alejandra.defaultPackage."${system}"
]
# using linux is highly recommended as cntr is amazing for debugging builds # using linux is highly recommended as cntr is amazing for debugging builds
++ lib.optionals stdenv.isLinux [ cntr ]; ++ lib.optionals pkgs.stdenv.isLinux [ pkgs.cntr ];
shellHook = '' shellHook =
export NIX_PATH=nixpkgs=${nixpkgs} # TODO: enable this once code base is formatted
export d2nExternalDir=${externalDirFor."${system}"} # self.checks.${system}.pre-commit-check.shellHook
export dream2nixWithExternals=${dream2nixFor."${system}".dream2nixWithExternals} ''
export NIX_PATH=nixpkgs=${nixpkgs}
export d2nExternalDir=${externalDirFor."${system}"}
export dream2nixWithExternals=${dream2nixFor."${system}".dream2nixWithExternals}
if [ -e ./overrides ]; then if [ -e ./overrides ]; then
export d2nOverridesDir=$(realpath ./overrides) export d2nOverridesDir=$(realpath ./overrides)
else else
export d2nOverridesDir=${./overrides} export d2nOverridesDir=${./overrides}
echo -e "\nManually execute 'export d2nOverridesDir={path to your dream2nix overrides dir}'" echo -e "\nManually execute 'export d2nOverridesDir={path to your dream2nix overrides dir}'"
fi fi
if [ -e ../dream2nix ]; then if [ -e ../dream2nix ]; then
export dream2nixWithExternals=$(realpath ./src) export dream2nixWithExternals=$(realpath ./src)
else else
export dream2nixWithExternals=${./src} export dream2nixWithExternals=${./src}
echo -e "\nManually execute 'export dream2nixWithExternals={path to your dream2nix checkout}'" echo -e "\nManually execute 'export dream2nixWithExternals={path to your dream2nix checkout}'"
fi fi
''; '';
}); });
checks = forAllSystems (system: pkgs: import ./tests/pure { checks = l.recursiveUpdate
inherit lib pkgs; (forAllSystems (system: pkgs:
dream2nix = dream2nixFor."${system}"; (import ./tests/pure {
}); inherit lib pkgs;
dream2nix = dream2nixFor."${system}";
})))
{}
# TODO: enable this once code base is formatted
# (forAllSystems (system: pkgs:{
# pre-commit-check =
# pre-commit-hooks.lib.${system}.run {
# src = ./.;
# hooks = {
# treefmt = {
# enable = true;
# name = "treefmt";
# pass_filenames = false;
# entry = l.toString (pkgs.writeScript "treefmt" ''
# #!${pkgs.bash}/bin/bash
# export PATH="$PATH:${alejandra.defaultPackage.${system}}/bin"
# ${pkgs.treefmt}/bin/treefmt --fail-on-change
# '');
# };
# };
# };
# }))
;
}; };
} }

View File

@ -30,7 +30,7 @@ let
] ]
'' ''
S=$(mktemp) S=$(mktemp)
async -s=$S server --start -j4 async -s=$S server --start -j$(nproc)
for test in ${toString allTests}; do for test in ${toString allTests}; do
async -s=$S cmd -- $test async -s=$S cmd -- $test

4
treefmt.toml Normal file
View File

@ -0,0 +1,4 @@
[formatter.nix]
command = "alejandra"
includes = ["*.nix"]
excludes = ["overrides/nodejs/electron/*"]