|
| 1 | +{ |
| 2 | + inputs = { |
| 3 | + nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; |
| 4 | + flake-parts.url = "github:hercules-ci/flake-parts"; |
| 5 | + pre-commit-hooks-nix.url = "github:cachix/pre-commit-hooks.nix"; |
| 6 | + pre-commit-hooks-nix.inputs.nixpkgs.follows = "nixpkgs"; |
| 7 | + hercules-ci-effects.url = "github:hercules-ci/hercules-ci-effects"; |
| 8 | + }; |
| 9 | + |
| 10 | + outputs = inputs@{ flake-parts, ... }: |
| 11 | + flake-parts.lib.mkFlake { inherit inputs; } |
| 12 | + { |
| 13 | + systems = [ "x86_64-linux" "x86_64-darwin" ]; |
| 14 | + imports = [ |
| 15 | + inputs.pre-commit-hooks-nix.flakeModule |
| 16 | + inputs.hercules-ci-effects.flakeModule |
| 17 | + |
| 18 | + ./pre-commit.nix |
| 19 | + ]; |
| 20 | + perSystem = { pkgs, config, ... }: |
| 21 | + { |
| 22 | + packages = { |
| 23 | + default = |
| 24 | + let |
| 25 | + packageJson = builtins.fromJSON (builtins.readFile ./package.json); |
| 26 | + in |
| 27 | + pkgs.buildNpmPackage { |
| 28 | + # See: https://github.com/NixOS/nixpkgs/tree/master/pkgs/build-support/node/build-npm-package |
| 29 | + # for helpful documentation |
| 30 | + pname = packageJson.name; |
| 31 | + version = packageJson.version; |
| 32 | + src = ./.; |
| 33 | + npmDepsHash = "sha256-wSSYPXAfsoqghsYu5quUZCyimVHtWu+Cp7yee7qAi7E="; |
| 34 | + }; |
| 35 | + |
| 36 | + # Tarball created from `npm pack` |
| 37 | + tgz = config.packages.default.overrideAttrs (_self: (super: { |
| 38 | + name = "${super.pname}-${super.version}.tgz"; |
| 39 | + makeCacheWritable = true; |
| 40 | + installPhase = |
| 41 | + '' |
| 42 | + tgzFile=$(npm --log-level=verbose pack | tail -n 1) |
| 43 | + mv $tgzFile $out |
| 44 | + ''; |
| 45 | + })); |
| 46 | + |
| 47 | + }; |
| 48 | + |
| 49 | + # Provides a development environment |
| 50 | + devShells = { |
| 51 | + default = config.packages.default.overrideAttrs (_self: (_super: { |
| 52 | + # What does this do? |
| 53 | + # `buildNpmPackage` is creates an npm cache in the |
| 54 | + # directory `$npmDeps`, so we tell `npm` to use that cache |
| 55 | + # when building things. |
| 56 | + # Unfortunately, that cache is in the nix store (and hence |
| 57 | + # cannot be written to), so we copy it out to a temporary |
| 58 | + # directory and point npm to the temporary directory. |
| 59 | + shellHook = |
| 60 | + '' |
| 61 | + # Copy the cache produced by nix somewhere else |
| 62 | + # s.t. npm may write to it |
| 63 | + TMP_DIR=$(mktemp -d) |
| 64 | + cp -r $npmDeps/. $TMP_DIR |
| 65 | + export NPM_CONFIG_CACHE=$TMP_DIR |
| 66 | + ''; |
| 67 | + })); |
| 68 | + }; |
| 69 | + |
| 70 | + # Runs `npm test` |
| 71 | + checks.default = |
| 72 | + config.packages.default.overrideAttrs (_self: (_super: { |
| 73 | + postBuild = |
| 74 | + '' |
| 75 | + npm --log-level=verbose test |
| 76 | + ''; |
| 77 | + })); |
| 78 | + }; |
| 79 | + }; |
| 80 | +} |
0 commit comments