From 44d628b6d913e041e6cfb76a010156afad744102 Mon Sep 17 00:00:00 2001 From: Bryan Bennett Date: Wed, 17 Jan 2024 08:23:08 -0500 Subject: [PATCH 1/6] Ignore result symlink --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 00b777f..cd098b8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ _build/ -_coverage/ \ No newline at end of file +_coverage/ +result \ No newline at end of file From ce8b85584a45883ba8427ff152052d7877e2b2a0 Mon Sep 17 00:00:00 2001 From: Bryan Bennett Date: Wed, 17 Jan 2024 08:22:39 -0500 Subject: [PATCH 2/6] Add pre-commit-hooks.nix integration --- .gitignore | 3 +- .pre-commit-config.yaml | 1 + flake.lock | 127 +++++++++++++++++++++++++++++++++++++++- flake.nix | 59 +++++++++++++------ 4 files changed, 170 insertions(+), 20 deletions(-) create mode 120000 .pre-commit-config.yaml diff --git a/.gitignore b/.gitignore index cd098b8..65c12c2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ _build/ _coverage/ -result \ No newline at end of file +result +/.pre-commit-config.yaml \ No newline at end of file diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 120000 index 0000000..2a19533 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1 @@ +/nix/store/y3cj5g3rd064bxpi3r2i1k2nzhk9qrdw-pre-commit-config.json \ No newline at end of file diff --git a/flake.lock b/flake.lock index a1e8315..24f924c 100644 --- a/flake.lock +++ b/flake.lock @@ -1,5 +1,21 @@ { "nodes": { + "flake-compat": { + "flake": false, + "locked": { + "lastModified": 1696426674, + "narHash": "sha256-kvjfFW7WAETZlt09AgDn1MrtKzP7t90Vf7vypd3OL1U=", + "owner": "edolstra", + "repo": "flake-compat", + "rev": "0f9255e01c2351cc7d116c072cb317785dd33b33", + "type": "github" + }, + "original": { + "owner": "edolstra", + "repo": "flake-compat", + "type": "github" + } + }, "flake-parts": { "inputs": { "nixpkgs-lib": [ @@ -20,6 +36,45 @@ "type": "github" } }, + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1701680307, + "narHash": "sha256-kAuep2h5ajznlPMD9rnQyffWG8EM/C73lejGofXvdM8=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "4022d587cbbfd70fe950c1e2083a02621806a725", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "gitignore": { + "inputs": { + "nixpkgs": [ + "pre-commit-hooks", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1703887061, + "narHash": "sha256-gGPa9qWNc6eCXT/+Z5/zMkyYOuRZqeFZBDbopNZQkuY=", + "owner": "hercules-ci", + "repo": "gitignore.nix", + "rev": "43e1aa1308018f37118e34d3a9cb4f5e75dc11d5", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "gitignore.nix", + "type": "github" + } + }, "nix-filter": { "locked": { "lastModified": 1701697642, @@ -51,11 +106,81 @@ "type": "github" } }, + "nixpkgs-stable": { + "locked": { + "lastModified": 1704874635, + "narHash": "sha256-YWuCrtsty5vVZvu+7BchAxmcYzTMfolSPP5io8+WYCg=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "3dc440faeee9e889fe2d1b4d25ad0f430d449356", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-23.11", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs_2": { + "locked": { + "lastModified": 1704842529, + "narHash": "sha256-OTeQA+F8d/Evad33JMfuXC89VMetQbsU4qcaePchGr4=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "eabe8d3eface69f5bb16c18f8662a702f50c20d5", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "pre-commit-hooks": { + "inputs": { + "flake-compat": "flake-compat", + "flake-utils": "flake-utils", + "gitignore": "gitignore", + "nixpkgs": "nixpkgs_2", + "nixpkgs-stable": "nixpkgs-stable" + }, + "locked": { + "lastModified": 1705229514, + "narHash": "sha256-itILy0zimR/iyUGq5Dgg0fiW8plRDyxF153LWGsg3Cw=", + "owner": "cachix", + "repo": "pre-commit-hooks.nix", + "rev": "ffa9a5b90b0acfaa03b1533b83eaf5dead819a05", + "type": "github" + }, + "original": { + "owner": "cachix", + "repo": "pre-commit-hooks.nix", + "type": "github" + } + }, "root": { "inputs": { "flake-parts": "flake-parts", "nix-filter": "nix-filter", - "nixpkgs": "nixpkgs" + "nixpkgs": "nixpkgs", + "pre-commit-hooks": "pre-commit-hooks" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" } } }, diff --git a/flake.nix b/flake.nix index 46b9ebb..4caf913 100644 --- a/flake.nix +++ b/flake.nix @@ -6,30 +6,43 @@ url = "github:hercules-ci/flake-parts"; inputs.nixpkgs-lib.follows = "nixpkgs"; }; - nix-filter = { - url = "github:numtide/nix-filter"; - }; + pre-commit-hooks = { url = "github:cachix/pre-commit-hooks.nix"; }; + nix-filter = { url = "github:numtide/nix-filter"; }; }; - outputs = inputs @ { flake-parts, nix-filter, ... }: - flake-parts.lib.mkFlake { inherit inputs; } - ({ lib, ... }: { - systems = [ - "aarch64-linux" - "x86_64-linux" + outputs = + inputs @ { flake-parts + , nix-filter + , pre-commit-hooks + , ... + }: + flake-parts.lib.mkFlake { inherit inputs; } ({ lib, ... }: { + systems = [ + "aarch64-linux" + "x86_64-linux" - "x86_64-darwin" - "aarch64-darwin" - ]; - perSystem = { config, pkgs, self', ... }: { + "x86_64-darwin" + "aarch64-darwin" + ]; + imports = [ pre-commit-hooks.flakeModule ]; + perSystem = + { config + , pkgs + , self' + , ... + }: { packages = { - flake_env = pkgs.ocamlPackages.callPackage ./default.nix { inherit nix-filter; }; + flake_env = pkgs.ocamlPackages.callPackage ./default.nix { + inherit nix-filter; + }; default = config.packages.flake_env; }; devShells.default = pkgs.mkShell { inputsFrom = [ self'.packages.default ]; packages = [ pkgs.just + pkgs.rnix-lsp + pkgs.pre-commit pkgs.ocamlPackages.alcotest pkgs.ocamlPackages.bisect_ppx pkgs.ocamlPackages.dune_3 @@ -39,12 +52,22 @@ pkgs.ocamlPackages.ocamlformat pkgs.ocamlPackages.ocamlformat-rpc-lib ]; + shellHook = config.pre-commit.installationScript; + }; + pre-commit = { + check.enable = true; + settings.hooks = { + nixpkgs-fmt.enable = true; + dune-fmt.enable = true; + }; }; }; - flake = { - overlays.default = final: _prev: { - flake_env = final.ocamlPackages.callPackage ./default.nix { inherit nix-filter; }; + flake = { + overlays.default = final: _prev: { + flake_env = final.ocamlPackages.callPackage ./default.nix { + inherit nix-filter; }; }; - }); + }; + }); } From 02a4503106a326438dcb55fbc28907c3963728db Mon Sep 17 00:00:00 2001 From: Bryan Bennett Date: Wed, 17 Jan 2024 08:26:23 -0500 Subject: [PATCH 3/6] nixpkgs-fmt default.nix --- default.nix | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/default.nix b/default.nix index 22c104a..21e15eb 100644 --- a/default.nix +++ b/default.nix @@ -13,6 +13,7 @@ , re , reason , sha +, }: buildDunePackage { pname = "flake_env"; @@ -41,23 +42,9 @@ buildDunePackage { postInstall = '' install -m400 -D direnvrc $out/share/flake_env/direnvrc ''; - checkInputs = [ - alcotest - bisect_ppx - ]; - nativeBuildInputs = [ - reason - ]; - propagatedBuildInputs = [ - core - core_unix - findlib - ocaml - ppx_yojson_conv - ppx_yojson_conv_lib - re - sha - ]; + checkInputs = [ alcotest bisect_ppx ]; + nativeBuildInputs = [ reason ]; + propagatedBuildInputs = [ core core_unix findlib ocaml ppx_yojson_conv ppx_yojson_conv_lib re sha ]; meta = with lib; { description = "Yet another flake plugin for direnv"; From 98c3131f7f3b49b65beef32f9be9cc7871ffc4d7 Mon Sep 17 00:00:00 2001 From: Bryan Bennett Date: Fri, 26 Jan 2024 07:28:59 -0500 Subject: [PATCH 4/6] Reduce dune version to work with Nix 23.11 --- .pre-commit-config.yaml | 1 - dune-project | 2 +- dune-workspace | 2 +- flake_env.opam | 2 +- 4 files changed, 3 insertions(+), 4 deletions(-) delete mode 120000 .pre-commit-config.yaml diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml deleted file mode 120000 index 2a19533..0000000 --- a/.pre-commit-config.yaml +++ /dev/null @@ -1 +0,0 @@ -/nix/store/y3cj5g3rd064bxpi3r2i1k2nzhk9qrdw-pre-commit-config.json \ No newline at end of file diff --git a/dune-project b/dune-project index f900cdb..88a33ec 100644 --- a/dune-project +++ b/dune-project @@ -1,4 +1,4 @@ -(lang dune 3.12) +(lang dune 3.11) (name flake_env) (generate_opam_files true) diff --git a/dune-workspace b/dune-workspace index a017b55..42130a0 100644 --- a/dune-workspace +++ b/dune-workspace @@ -1,2 +1,2 @@ -(lang dune 3.12) +(lang dune 3.11) (instrument_with bisect_ppx) \ No newline at end of file diff --git a/flake_env.opam b/flake_env.opam index 2a66339..106f38f 100644 --- a/flake_env.opam +++ b/flake_env.opam @@ -10,7 +10,7 @@ doc: "https://git.sr.ht/~bryan_bennett/flake_env" bug-reports: "https://todo.sr.ht/~bryan_bennett/flake_env" depends: [ "ocaml" - "dune" {>= "3.12"} + "dune" {>= "3.11"} "odoc" {with-doc} ] build: [ From 6b313a6ff67267e50ef3366d28f4422654f90b46 Mon Sep 17 00:00:00 2001 From: Bryan Bennett Date: Wed, 13 Mar 2024 21:42:17 -0400 Subject: [PATCH 5/6] Rework hash_files to return (str, str) result --- bin/flake_env.re | 9 ++++++++- lib/flake_env__util.re | 38 +++++++++++++++++++++++++----------- tests/flake_env_test_util.re | 35 +++++++++++++++++++++++---------- 3 files changed, 60 insertions(+), 22 deletions(-) diff --git a/bin/flake_env.re b/bin/flake_env.re index 4567cb9..d2b6dfd 100644 --- a/bin/flake_env.re +++ b/bin/flake_env.re @@ -12,7 +12,14 @@ let main = () => { switch (Lib.Watches.get()) { | Ok(watches) => let paths = Array.map(~f=watch => watch.path, watches); - let hash = Util.hash_files(paths); + let hash = + switch (Util.hash_files(paths)) { + | Ok(hsh) => hsh + | Error(msg) => + Printf.eprintf("%s\n", e); + exit(1); + }; + let profile = layout_directory ++ "/flake-profile-" ++ hash; let profile_rc = profile ++ ".rc"; diff --git a/lib/flake_env__util.re b/lib/flake_env__util.re index 1b5aa7b..e471010 100644 --- a/lib/flake_env__util.re +++ b/lib/flake_env__util.re @@ -17,23 +17,39 @@ let nix = args => ); let hash_files = filenames => { - /*** Hash all entries in [filenames], returning a hex-encoded string of the hash of their contents */ + /*** Hash all entries in [filenames] + Returns Some(hex-string) or None if no filenames are found. + */ let ctx = Sha1.init(); - let () = + let files_to_hash = filenames |> Array.filter(~f=f => switch (Sys_unix.file_exists(f)) { | `Yes => true - | _ => false + | _ => + // let fullpth = Filename_unix.realpath(f); + Printf.eprintf( + "Cannot find file %s (cwd: %s)\n", + f, + Core_unix.getcwd(), + ); + false; } - ) - |> Array.iter(~f=f => { - f - |> In_channel.create - |> In_channel.input_all - |> Sha1.update_string(ctx) - }); - Sha1.finalize(ctx) |> Sha1.to_hex; + ); + + switch (files_to_hash |> Array.length) { + | 0 => Error("No files found to hash") + | _ => + let () = + files_to_hash + |> Array.iter(~f=f => { + f + |> In_channel.create + |> In_channel.input_all + |> Sha1.update_string(ctx) + }); + Ok(Sha1.finalize(ctx) |> Sha1.to_hex); + }; }; let rec rmrf = path => { diff --git a/tests/flake_env_test_util.re b/tests/flake_env_test_util.re index e45ccaf..9444d8a 100644 --- a/tests/flake_env_test_util.re +++ b/tests/flake_env_test_util.re @@ -16,7 +16,22 @@ let _syst_to_bool = let check_exit_or_signal = Alcotest.(check(Alcotest.pair(testable_exit_or_signal, string))); -let check_string = Alcotest.(check(string)); +let testable_result_string = + Alcotest.testable( + (pp_fmt, elem) => { + switch (elem) { + | Ok(s) => Fmt.pf(pp_fmt, "Ok(%s)", s) + | Error(s) => Fmt.pf(pp_fmt, "Error(%s)", s) + } + }, + (a, b) => + switch (a, b) { + | (Ok(a), Ok(b)) => String.compare(a, b) == 0 + | (Error(a), Error(b)) => String.compare(a, b) == 0 + | _ => false + }, + ); +let check_result_string = Alcotest.(check(testable_result_string)); let check_bool = Alcotest.(check(bool)); let check_get_args = Alcotest.( @@ -47,26 +62,26 @@ let test_run_process_stdout = () => ); let test_hash_one = () => { - check_string( + check_result_string( "Hash matches", - hash_files([|"../LICENSE"|]), - "b43cf2e824eb66ba0e8f939c08072a8e307b5e5f", + Ok("32b4ac64be805d730745f6bac45a5d95174ebd10"), + hash_files([|"spit_version.sh"|]), ); }; let test_hash_multiple = () => { - check_string( + check_result_string( "Hash matches", - hash_files([|"../LICENSE", "../LICENSE"|]), - "08304d8baeed02722f81252952b00f6ac011ce0c", + Ok("e4c880fc6ab9a1b88e6be18e53fc4cec9f463d1a"), + hash_files([|"spit_version.sh", "spit_version.sh"|]), ); }; let test_hash_filters_nonexistent = () => { - check_string( + check_result_string( "Hash matches", - hash_files([|"../LICENSE", "FOOBARBAZ"|]), - "b43cf2e824eb66ba0e8f939c08072a8e307b5e5f", + Ok("32b4ac64be805d730745f6bac45a5d95174ebd10"), + hash_files([|"spit_version.sh", "FOOBARBAZ"|]), ); }; From 76e06049cffed82283cff181af424f5e1536d603 Mon Sep 17 00:00:00 2001 From: George Macon Date: Wed, 13 Mar 2024 20:16:05 -0400 Subject: [PATCH 6/6] Use . if no flake reference is specified --- direnvrc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/direnvrc b/direnvrc index 30d8434..85cc4bc 100644 --- a/direnvrc +++ b/direnvrc @@ -41,7 +41,7 @@ use_flake_env() { local ld=$(direnv_layout_dir) export direnv - eval "$(@flake_env@ "$ld" "$@")" + eval "$(@flake_env@ "${1:-"."}" "$ld")" export -n direnv