From 6e0eb50284df4f28cfb65f2e844b0bb5e54f8468 Mon Sep 17 00:00:00 2001 From: Bryan Bennett Date: Mon, 8 Jan 2024 14:52:39 -0500 Subject: [PATCH 01/21] Add .gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..00b777f --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +_build/ +_coverage/ \ No newline at end of file From 897ae138ea08edd8e383d9bb8a59d9e326990b5e Mon Sep 17 00:00:00 2001 From: Bryan Bennett Date: Mon, 8 Jan 2024 14:52:46 -0500 Subject: [PATCH 02/21] Lock ocamlformat version --- .ocamlformat | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .ocamlformat diff --git a/.ocamlformat b/.ocamlformat new file mode 100644 index 0000000..40137a6 --- /dev/null +++ b/.ocamlformat @@ -0,0 +1,2 @@ +version=0.26.1 +profile=default \ No newline at end of file From 21c8ff71e55f85c6100b626eb1c490428330a7f5 Mon Sep 17 00:00:00 2001 From: Bryan Bennett Date: Mon, 8 Jan 2024 14:54:03 -0500 Subject: [PATCH 03/21] Add simple justfile --- Justfile | 5 +++++ flake.nix | 1 + 2 files changed, 6 insertions(+) create mode 100644 Justfile diff --git a/Justfile b/Justfile new file mode 100644 index 0000000..9e7ec57 --- /dev/null +++ b/Justfile @@ -0,0 +1,5 @@ +build: + @dune build + +fmt: + @dune build @fmt --auto-promote diff --git a/flake.nix b/flake.nix index 12bcdb7..99af3a7 100644 --- a/flake.nix +++ b/flake.nix @@ -29,6 +29,7 @@ devShells.default = pkgs.mkShell { inputsFrom = [ self'.packages.default ]; packages = [ + pkgs.just pkgs.ocamlPackages.dune_3 pkgs.ocamlPackages.findlib pkgs.ocamlPackages.ocaml From 7759d2ac6aeedaa08b71e3e7e4132ea86d2707bb Mon Sep 17 00:00:00 2001 From: Bryan Bennett Date: Mon, 8 Jan 2024 14:57:59 -0500 Subject: [PATCH 04/21] Continue moving heavy-lifting into library --- bin/flake_env.re | 158 ++++++++----------------------------- direnvrc | 2 +- dune-project | 11 +-- flake_env.opam | 2 +- lib/flake_env__util.re | 60 ++++++++++++-- lib/flake_env__versions.re | 67 ++++++++-------- lib/lib.re | 70 ++++++++++++++++ 7 files changed, 194 insertions(+), 176 deletions(-) diff --git a/bin/flake_env.re b/bin/flake_env.re index e9845c3..2615e9c 100644 --- a/bin/flake_env.re +++ b/bin/flake_env.re @@ -1,149 +1,55 @@ open Core; module Unix = Core_unix; +open Lib; -let preflight = (layout_directory) => { - switch (Lib.Versions.preflight_versions(), Sys_unix.is_directory(layout_directory)) { - | (Ok(_), `Yes) => Ok() - | (Ok(_), _) => { - Unix.mkdir_p(layout_directory); - Ok() - } - | (err, _) => err - } -} - -let hash_files = (filenames) => { - let ctx = Sha1.init(); - let () = filenames - |> Array.filter(~f=f => switch (Sys_unix.file_exists(f)) { - | `Yes => true - | _ => false - }) - |> Array.iter(~f=(f) => { - f |> In_channel.create |> In_channel.input_all |> Sha1.update_string(ctx); - }); - Sha1.finalize(ctx) |> Sha1.to_hex -}; - -// TODO: Maybe make this more terse? -let rec rmrf = (path) => { - switch (Unix.lstat(path).st_kind) { - | exception Unix.Unix_error(_, _, _) => () - | S_REG => Unix.unlink(path) - | S_LNK => Unix.unlink(path) - | S_DIR => { - Sys_unix.readdir(path) |> Array.iter(~f=name => rmrf(Filename.concat(path, name))); - Unix.rmdir(path) - } - | S_CHR => Printf.eprintf("Don't know how to handle Character Device file\n") - | S_BLK => Printf.eprintf("Don't know how to handle Block Device file\n") - | S_FIFO => Printf.eprintf("Don't know how to handle FIFO file\n") - | S_SOCK => Printf.eprintf("Don't know how to handle Socket file\n") - } -}; - -let clean_old_gcroots = (layout_dir) => { - rmrf(layout_dir ++ "/flake-inputs/"); - rmrf(layout_dir); - Unix.mkdir_p(layout_dir ++ "/flake-inputs/"); -}; - -let print_cur_cache = (profile_rc) => { - In_channel.read_all(profile_rc) |> Printf.printf("%s") -}; - -let add_gcroot = (store_path, symlink) => { - switch (Lib.Util.nix(["build", "--out-link", symlink, store_path])) { - | (Ok(), _) => Ok() - | (err, _) => err - } -} - -let freshen_cache = (layout_dir, hash, flake_specifier) => { - clean_old_gcroots(layout_dir); - let tmp_profile = layout_dir ++ "flake-tmp-profile." ++ Core.Pid.to_string(Core_unix.getpid()); - let (exit_code, stdout_content) = Lib.Util.nix(["print-dev-env", "--profile", tmp_profile, flake_specifier]); - - let profile = layout_dir ++ "/flake-profile-" ++ hash; - let profile_rc = profile ++ ".rc"; - - switch (exit_code) { - | Ok() => { - Out_channel.with_file(~f=f=> Out_channel.output_string(f, stdout_content), profile_rc); - switch (add_gcroot(tmp_profile, profile)) { - | Ok() => { - Sys_unix.remove(tmp_profile); - let flake_input_cache_path = layout_dir ++ "/flake-inputs/" - let flake_inputs = Lib.Watches.get_input_paths(); - flake_inputs |> List.iter(~f=(inpt) => { - switch (add_gcroot("/nix/store/" ++ inpt, flake_input_cache_path ++ inpt)) { - | Ok() => () - | err => Printf.eprintf("Failed creating flake-input gcroot: %s\n", Core_unix.Exit_or_signal.to_string_hum(err)); - }; - }); - print_cur_cache(profile_rc); - } - | err => { - Printf.eprintf("Failed creating gcroot: %s\n", Core_unix.Exit_or_signal.to_string_hum(err)); - exit(1); - } - }; - } - | err => { - Printf.eprintf("Failed evaluating flake: %s\n", Core_unix.Exit_or_signal.to_string_hum(err)); - exit(1); - } - }; - -}; - -// TODO: Extend to add support for additional flags, maybe? let main = () => { let argv = Sys.get_argv(); - switch (Array.length(argv)) { - | 3 => { - let flake_specifier = argv[1]; - let layout_directory = argv[2]; + switch (Util.get_args(argv)) { + | Ok((layout_directory, flake_specifier, other_args)) => { switch (preflight(layout_directory)) { - | Ok() => { - switch (Lib.Watches.get()) { - | Ok(watches) => { - let paths = Array.map(~f=watch => watch.path, watches); - let hash = hash_files(paths); - let profile = layout_directory ++ "/flake-profile-" ++ hash; - let profile_rc = profile ++ ".rc"; + | Ok() => { + switch (Lib.Watches.get()) { + | Ok(watches) => { + let paths = Array.map(~f=watch => watch.path, watches); + let hash = Util.hash_files(paths); + let profile = layout_directory ++ "/flake-profile-" ++ hash; + let profile_rc = profile ++ ".rc"; - switch ((Sys_unix.is_file(profile_rc), Sys_unix.is_file(profile))) { - | (`Yes, `Yes) => { - let profile_rc_mtime = Unix.stat(profile_rc).st_mtime; - let all_older= Array.map(~f=watch => watch.modtime, watches) - |> Array.for_all(~f=watch_mtime => watch_mtime <= int_of_float(profile_rc_mtime)) - switch (all_older) { - | true => print_cur_cache(profile_rc) - | false => freshen_cache(layout_directory, hash, flake_specifier) - } + switch ((Sys_unix.is_file(profile_rc), Sys_unix.is_file(profile))) { + | (`Yes, `Yes) => { + let profile_rc_mtime = Unix.stat(profile_rc).st_mtime; + let all_older = Array.map( + ~f=watch => watch.modtime, watches) + |> Array.for_all( + ~f=watch_mtime => watch_mtime <= int_of_float(profile_rc_mtime) + ) + if (all_older) { + print_cur_cache(profile_rc) + } else { + freshen_cache(layout_directory, hash, flake_specifier, other_args) } - | _ => freshen_cache(layout_directory, hash, flake_specifier) } - }; - | Error(e) => { - Printf.eprintf("%s\n", e); - exit(1); + | _ => freshen_cache(layout_directory, hash, flake_specifier, other_args) } + }; + | Error(e) => { + Printf.eprintf("%s\n", e); + exit(1); } } + } | Error(e) => { Printf.eprintf("%s\n", e); exit(1); } }; } - | _ => { - Printf.eprintf("%s \n", argv[0]); + | Error() => { + Printf.eprintf("%s <...args>\n", argv[0]); exit(1); - } + } } -} +}; let () = main(); diff --git a/direnvrc b/direnvrc index c498f73..30d8434 100644 --- a/direnvrc +++ b/direnvrc @@ -41,7 +41,7 @@ use_flake_env() { local ld=$(direnv_layout_dir) export direnv - eval "$(@flake_env@ "$1" "$ld")" + eval "$(@flake_env@ "$ld" "$@")" export -n direnv diff --git a/dune-project b/dune-project index 063ff61..f900cdb 100644 --- a/dune-project +++ b/dune-project @@ -1,18 +1,11 @@ -(lang dune 3.11) +(lang dune 3.12) (name flake_env) - (generate_opam_files true) - -(source - (sourcehut bryan_bennett/flake_env)) - +(source (sourcehut bryan_bennett/flake_env)) (authors "Bryan Bennett") - (maintainers "Bryan Bennett") - (license MIT) - (documentation https://git.sr.ht/~bryan_bennett/flake_env) (package diff --git a/flake_env.opam b/flake_env.opam index 106f38f..2a66339 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.11"} + "dune" {>= "3.12"} "odoc" {with-doc} ] build: [ diff --git a/lib/flake_env__util.re b/lib/flake_env__util.re index e05c1fa..0e8dfc5 100644 --- a/lib/flake_env__util.re +++ b/lib/flake_env__util.re @@ -1,10 +1,60 @@ open Core; module Unix = Core_unix; -let nix = (args) => { - let stdout_chan = Unix.open_process_in( - "nix --extra-experimental-features \"nix-command flakes\" " ++ (args |> String.concat(~sep=" "))); +let run_process = (name, args) => { + /*** Run a process [name] with args [args], returning (exit_code, stdout text) */ + let stdout_chan = + Unix.open_process_in(name ++ " " ++ (args |> String.concat(~sep=" "))); let stdout_content = stdout_chan |> In_channel.input_all; let exit_code = Unix.close_process_in(stdout_chan); - (exit_code, stdout_content) -} + (exit_code, stdout_content); +}; + +let nix = args => + run_process( + "nix", + ["--extra-experimental-features", "\"nix-command flakes\" ", ...args], + ); + +let hash_files = filenames => { + /*** Hash all entries in [filenames], returning a hex-encoded string of the hash of their contents */ + let ctx = Sha1.init(); + let () = + filenames + |> Array.filter(~f=f => + switch (Sys_unix.file_exists(f)) { + | `Yes => true + | _ => false + } + ) + |> Array.iter(~f=f => { + f + |> In_channel.create + |> In_channel.input_all + |> Sha1.update_string(ctx) + }); + Sha1.finalize(ctx) |> Sha1.to_hex; +}; + +let rec rmrf = path => { + switch (Unix.lstat(path).st_kind) { + | exception (Unix.Unix_error(_, _, _)) => () + | S_REG | S_LNK => Unix.unlink(path) + | S_DIR => + Sys_unix.readdir(path) + |> Array.iter(~f=name => rmrf(Filename.concat(path, name))); + Unix.rmdir(path); + | _ => Printf.eprintf("Unsupported file type (Chr or Block device, FIFO, or Socket)\n") + }; +}; + +let get_args = argv => { + switch (Array.length(argv)) { + | x when x >= 3 => + let layout_directory = argv[1]; + let flake_specifier = argv[2]; + let other_args = snd(List.split_n(List.of_array(argv), 3)); + Ok((layout_directory, flake_specifier, other_args)); + | _ => Error() + }; +}; diff --git a/lib/flake_env__versions.re b/lib/flake_env__versions.re index 2430daf..0ad683b 100644 --- a/lib/flake_env__versions.re +++ b/lib/flake_env__versions.re @@ -1,28 +1,26 @@ open Core; module Unix = Core_unix; +module Util = Flake_env__util; + + type t = { major: int, minor: int, point: int, }; +let init = (major, minor, point) => { + {major, minor, point} +}; + +let required_direnv_version = init(2, 21, 3); + +let required_nix_version = init(2, 10, 0); + let semver_re = Re.compile(Re.Posix.re({|([0-9]+)\.([0-9]+)\.([0-9]+)|})); -let required_direnv_version = { - major: 2, - minor: 21, - point: 3 -}; - -let required_nix_version = { - major: 2, - minor: 10, - point: 0 -}; - -// TODO: test this -let compare_version_number = (a, b) => { +let compare = (a, b) => { switch (a, b) { | (a, b) when a.major == b.major && a.minor == b.minor && a.point == b.point => 0 | (a, b) when a.major < b.major => -1 @@ -32,46 +30,47 @@ let compare_version_number = (a, b) => { } } -let%test_unit "compare_version_number" = [%test_eq: int](compare_version_number({major: 1, minor: 0, point: 0}, {major: 2, minor: 0, point: 0}), -1); - let extract_version_number = (cmd) => { - let full_cmd = cmd ++ " --version"; - switch (Core_unix.open_process_in(full_cmd) |> In_channel.input_line) { - | Some(stdout) => { + switch (Util.run_process(cmd, ["--version"])) { + | (Ok(), stdout) when String.length(stdout) > 0 => { let substrings = Re.exec(semver_re, stdout); let groups = Re.Group.all(substrings); - Ok({ - major: groups[1] |> int_of_string, - minor: groups[2] |> int_of_string, - point: groups[3] |> int_of_string - }) + if ((groups |> Array.length) == 4) { + Ok({ + major: groups[1] |> int_of_string, + minor: groups[2] |> int_of_string, + point: groups[3] |> int_of_string + }) + } else { + Error(Printf.sprintf("Stdout did not contain a version number for `%s --version`", cmd)) + } } - | None => Error(Printf.sprintf("Failed executing '%s'\n", cmd)) + | _ => Error(Printf.sprintf("Failed executing '%s'\n", cmd)) } }; -let is_version_new_enough = (cur, needed) => { +let is_new_enough = (cur, needed) => { switch (cur) { | Ok(cur) => { - switch (compare_version_number(cur, needed)) { + switch (compare(cur, needed)) { | x when x < 0 => Ok(false) | _ => Ok(true) } } | Error(e) => Error(e) } -} +}; -let preflight_versions = () => { - let in_direnv = switch (Sys.getenv("direnv")) { +let in_direnv = () => switch (Sys.getenv("direnv")) { | Some(_) => true | None => false - }; +}; - let is_nix_new_enough = is_version_new_enough(extract_version_number("nix"), required_nix_version); - let is_direnv_new_enough = is_version_new_enough(extract_version_number("direnv"), required_direnv_version); +let preflight_versions = () => { + let is_nix_new_enough = is_new_enough(extract_version_number("nix"), required_nix_version); + let is_direnv_new_enough = is_new_enough(extract_version_number("direnv"), required_direnv_version); - switch (in_direnv, is_direnv_new_enough, is_nix_new_enough) { + switch (in_direnv(), is_direnv_new_enough, is_nix_new_enough) { | (false, _, _) => Error("Not in direnv!") | (_, Ok(false), _) => Error("Direnv version is not new enough") | (_, _, Ok(false)) => Error("Nix version is not new enough") diff --git a/lib/lib.re b/lib/lib.re index 1198207..a9a1f6e 100644 --- a/lib/lib.re +++ b/lib/lib.re @@ -1,3 +1,73 @@ +open Core; +module Unix = Core_unix; + module Util = Flake_env__util; module Watches = Flake_env__watches; module Versions = Flake_env__versions; + +let print_cur_cache = (profile_rc) => { + In_channel.read_all(profile_rc) |> Printf.printf("%s") +}; + +let clean_old_gcroots = (layout_dir) => { + Util.rmrf(layout_dir ++ "/flake-inputs/"); + Util.rmrf(layout_dir); + Unix.mkdir_p(layout_dir ++ "/flake-inputs/"); +}; + +let add_gcroot = (store_path, symlink) => { + switch (Util.nix(["build", "--out-link", symlink, store_path])) { + | (Ok(), _) => Ok() + | (err, _) => err + } +}; + +let freshen_cache = (layout_dir, hash, flake_specifier, other_args) => { + clean_old_gcroots(layout_dir); + let tmp_profile = layout_dir ++ "flake-tmp-profile." ++ Core.Pid.to_string(Core_unix.getpid()); + + let pde_args = ["print-dev-env", "--profile", tmp_profile, flake_specifier, ...other_args]; + let (exit_code, stdout_content) = Util.nix(pde_args); + + let profile = layout_dir ++ "/flake-profile-" ++ hash; + let profile_rc = profile ++ ".rc"; + + switch (exit_code) { + | Ok() => { + Out_channel.with_file(~f=f=> Out_channel.output_string(f, stdout_content), profile_rc); + switch (add_gcroot(tmp_profile, profile)) { + | Ok() => { + Sys_unix.remove(tmp_profile); + let flake_input_cache_path = layout_dir ++ "/flake-inputs/" + let flake_inputs = Watches.get_input_paths(); + flake_inputs |> List.iter(~f=(inpt) => { + switch (add_gcroot("/nix/store/" ++ inpt, flake_input_cache_path ++ inpt)) { + | Ok() => () + | err => Printf.eprintf("Failed creating flake-input gcroot: %s\n", Core_unix.Exit_or_signal.to_string_hum(err)); + }; + }); + print_cur_cache(profile_rc); + } + | err => { + Printf.eprintf("Failed creating gcroot: %s\n", Core_unix.Exit_or_signal.to_string_hum(err)); + exit(1); + } + }; + } + | err => { + Printf.eprintf("Failed evaluating flake: %s\n", Core_unix.Exit_or_signal.to_string_hum(err)); + exit(1); + } + }; + }; + +let preflight = (layout_directory) => { + switch (Versions.preflight_versions(), Sys_unix.is_directory(layout_directory)) { + | (Ok(_), `Yes) => Ok() + | (Ok(_), _) => { + Unix.mkdir_p(layout_directory); + Ok() + } + | (err, _) => err + } +} From 04109672f0e59480641cf9d60cb02d6dd2bc5b07 Mon Sep 17 00:00:00 2001 From: Bryan Bennett Date: Mon, 8 Jan 2024 14:59:28 -0500 Subject: [PATCH 05/21] Add tests --- dune-workspace | 2 + flake.nix | 2 + lib/dune | 12 ++- tests/dune | 13 +++ tests/flake_env_test_util.re | 159 ++++++++++++++++++++++++++++ tests/flake_env_test_versions.re | 174 +++++++++++++++++++++++++++++++ tests/flake_env_test_watches.re | 41 ++++++++ tests/spit_version.sh | 2 + 8 files changed, 403 insertions(+), 2 deletions(-) create mode 100644 dune-workspace create mode 100644 tests/dune create mode 100644 tests/flake_env_test_util.re create mode 100644 tests/flake_env_test_versions.re create mode 100644 tests/flake_env_test_watches.re create mode 100755 tests/spit_version.sh diff --git a/dune-workspace b/dune-workspace new file mode 100644 index 0000000..a017b55 --- /dev/null +++ b/dune-workspace @@ -0,0 +1,2 @@ +(lang dune 3.12) +(instrument_with bisect_ppx) \ No newline at end of file diff --git a/flake.nix b/flake.nix index 99af3a7..46b9ebb 100644 --- a/flake.nix +++ b/flake.nix @@ -30,6 +30,8 @@ inputsFrom = [ self'.packages.default ]; packages = [ pkgs.just + pkgs.ocamlPackages.alcotest + pkgs.ocamlPackages.bisect_ppx pkgs.ocamlPackages.dune_3 pkgs.ocamlPackages.findlib pkgs.ocamlPackages.ocaml diff --git a/lib/dune b/lib/dune index 6e33744..e268194 100644 --- a/lib/dune +++ b/lib/dune @@ -1,5 +1,13 @@ (library (name lib) (public_name flake_env.lib) - (libraries core core_unix core_unix.filename_unix core_unix.sys_unix ppx_yojson_conv re sha) - (preprocess (pps ppx_yojson_conv ppx_jane ppx_inline_test))) \ No newline at end of file + (libraries + core + core_unix + core_unix.filename_unix + core_unix.sys_unix + ppx_yojson_conv + re + sha) + (instrumentation (backend bisect_ppx)) + (preprocess (pps ppx_yojson_conv ppx_jane ))) \ No newline at end of file diff --git a/tests/dune b/tests/dune new file mode 100644 index 0000000..9440d3f --- /dev/null +++ b/tests/dune @@ -0,0 +1,13 @@ +(tests + (names + flake_env_test_versions + flake_env_test_watches + flake_env_test_util) + (deps spit_version.sh) + (libraries + lib + alcotest + core + core_unix + core_unix.sys_unix + core_unix.filename_unix)) \ No newline at end of file diff --git a/tests/flake_env_test_util.re b/tests/flake_env_test_util.re new file mode 100644 index 0000000..6f8e11f --- /dev/null +++ b/tests/flake_env_test_util.re @@ -0,0 +1,159 @@ +open Core; +module Unix = Core_unix; + +open Lib.Util; + +let _pp_exit_or_signal = (pp_fmt, e) => + Fmt.pf(pp_fmt, "%s", Unix.Exit_or_signal.to_string_hum(e)); +let _exit_or_signal_eq = (a, b) => Unix.Exit_or_signal.compare(a, b) == 0; +let testable_exit_or_signal = + Alcotest.testable(_pp_exit_or_signal, _exit_or_signal_eq); + +let _syst_to_bool = + fun + | `Yes => true + | _ => false; + +let check_exit_or_signal = + Alcotest.(check(Alcotest.pair(testable_exit_or_signal, string))); +let check_string = Alcotest.(check(string)); +let check_bool = Alcotest.(check(bool)); +let check_get_args = + Alcotest.( + check( + Alcotest.result(Alcotest.triple(string, string, list(string)), unit), + ) + ); + +let test_run_process_success = () => + check_exit_or_signal( + "Returns expected", + (Ok(), ""), + run_process("true", []), + ); + +let test_run_process_failure = () => + check_exit_or_signal( + "Returns expected", + (Error(`Exit_non_zero(1)), ""), + run_process("false", []), + ); + +let test_run_process_stdout = () => + check_exit_or_signal( + "Returns expected", + (Ok(), "echoed\n"), + run_process("echo", ["echoed"]), + ); + +let test_hash_one = () => { + check_string( + "Hash matches", + hash_files([|"../LICENSE"|]), + "b43cf2e824eb66ba0e8f939c08072a8e307b5e5f", + ); +}; + +let test_hash_multiple = () => { + check_string( + "Hash matches", + hash_files([|"../LICENSE", "../LICENSE"|]), + "08304d8baeed02722f81252952b00f6ac011ce0c", + ); +}; + +let test_hash_filters_nonexistent = () => { + check_string( + "Hash matches", + hash_files([|"../LICENSE", "FOOBARBAZ"|]), + "b43cf2e824eb66ba0e8f939c08072a8e307b5e5f", + ); +}; + +let test_rmrf_file = () => { + let tmp_file_name = Filename_unix.temp_file("test", "txt"); + rmrf(tmp_file_name); + + check_bool( + "File removed", + false, + _syst_to_bool(Sys_unix.is_file(tmp_file_name)), + ); +}; + +let test_rmrf_dir = () => { + let temp_dir_name = Filename_unix.temp_dir("test", "d"); + let _ = Filename_unix.temp_file(~in_dir=temp_dir_name, "test", "txt"); + + rmrf(temp_dir_name); + + check_bool( + "File removed", + false, + _syst_to_bool(Sys_unix.file_exists(temp_dir_name)), + ); +}; + +let test_get_args_simple = () => { + check_get_args( + "Parses successfully", + Ok(("foo", "bar", ["oof", "rab", "zab"])), + get_args([|"000", "foo", "bar", "oof", "rab", "zab"|]), + ); +}; + +let test_get_args_just_enough = () => { + check_get_args( + "Parses just enough args", + Ok(("foo", "bar", [])), + get_args([|"000", "foo", "bar"|]), + ); +}; + +let test_get_args_error = () => { + check_get_args("Errors on too few args", Error(), get_args([|"000", "111"|])) +}; + +let () = + Alcotest.( + run( + "Watches", + [ + ( + "run_process", + [ + test_case("Capture's Stdout", `Quick, test_run_process_stdout), + test_case("Success", `Quick, test_run_process_success), + test_case("Failure", `Quick, test_run_process_failure), + ], + ), + ( + "hash_files", + [ + test_case("Hashes one file", `Quick, test_hash_one), + test_case("Hashes multiple files", `Quick, test_hash_multiple), + test_case( + "Filters non-existent", + `Quick, + test_hash_filters_nonexistent, + ), + ], + ), + ( + "rmrf helper", + [ + test_case("Removes file", `Quick, test_rmrf_file), + test_case("Removes dir", `Quick, test_rmrf_dir), + ], + ), + ( + "get_args", + [ + test_case("Parses Args", `Quick, test_get_args_simple), + test_case("Parses just enough args", `Quick, test_get_args_just_enough), + test_case("Handles too few args", `Quick, test_get_args_error) + ], + ), + ], + ) + ); diff --git a/tests/flake_env_test_versions.re b/tests/flake_env_test_versions.re new file mode 100644 index 0000000..5b06a48 --- /dev/null +++ b/tests/flake_env_test_versions.re @@ -0,0 +1,174 @@ +open Lib; + +let pprint = (pp_fmt, version) => { + Versions.( + Fmt.pf( + pp_fmt, + "{ major: %d, minor: %d, point: %d }", + version.major, + version.minor, + version.point, + ) + ); +}; +let testable_version = + Alcotest.testable(pprint, (a, b) => Versions.compare(a, b) == 0); + +let check_version = + Alcotest.(check(Alcotest.result(testable_version, string))); + +let test_compare_equal = () => { + let a = Versions.init(1, 0, 0); + Alcotest.(check(int))("equal", 0, Versions.compare(a, a)); +}; + +let test_compare_first_major_greater = () => { + let a = Versions.init(2, 0, 0); + let b = Versions.init(1, 0, 0); + Alcotest.(check(int))("First major greater", 1, Versions.compare(a, b)); +}; + +let test_compare_first_minor_greater = () => { + let a = Versions.init(1, 1, 0); + let b = Versions.init(1, 0, 0); + Alcotest.(check(int))("First minor greater", 1, Versions.compare(a, b)); +}; + +let test_compare_first_point_greater = () => { + let a = Versions.init(1, 0, 1); + let b = Versions.init(1, 0, 0); + Alcotest.(check(int))("First point greater", 1, Versions.compare(a, b)); +}; + +let test_compare_second_major_greater = () => { + let a = Versions.init(1, 0, 0); + let b = Versions.init(2, 0, 0); + Alcotest.(check(int))("Second major greater", -1, Versions.compare(a, b)); +}; + +let test_compare_second_minor_greater = () => { + let a = Versions.init(1, 0, 0); + let b = Versions.init(1, 1, 0); + Alcotest.(check(int))("Second minor greater", -1, Versions.compare(a, b)); +}; + +let test_compare_second_point_greater = () => { + let a = Versions.init(1, 0, 0); + let b = Versions.init(1, 0, 1); + Alcotest.(check(int))("Second point greater", -1, Versions.compare(a, b)); +}; + +let test_ine_cur_newer = () => { + let a = Ok(Versions.init(2, 0, 0)); + let b = Versions.init(1, 0, 0); + let ine = Versions.is_new_enough(a, b); + Alcotest.(check(bool))("Curr newer", true, ine |> Result.get_ok); +}; + +let test_ine_cur_older = () => { + let a = Ok(Versions.init(1, 0, 0)); + let b = Versions.init(2, 0, 0); + let ine = Versions.is_new_enough(a, b); + Alcotest.(check(bool))("Curr older", false, ine |> Result.get_ok); +}; + +let test_ine_cur_equal = () => { + let a = Versions.init(1, 0, 0); + let ine = Versions.is_new_enough(Ok(a), a); + Alcotest.(check(bool))("Curr equal", true, ine |> Result.get_ok); +}; + +let test_ine_error = () => { + let a = Error("foobarbaz"); + let ine = Versions.is_new_enough(a, Versions.init(1, 0, 0)); + Alcotest.(check(Alcotest.result(bool, string)))( + "Error bubbled", + Error("foobarbaz"), + ine, + ); +}; + +let test_in_direnv_true = () => { + Core_unix.putenv(~key="direnv", ~data="direnv"); + Alcotest.(check(bool))("In direnv", true, Versions.in_direnv()); +}; + +let test_in_direnv_false = () => { + Core_unix.unsetenv("direnv"); + Alcotest.(check(bool))("Not in direnv", false, Versions.in_direnv()); +}; + +let test_extract_version_number_success = () => { + let result = Versions.extract_version_number("../tests/spit_version.sh"); + check_version("Versions", Ok(Versions.init(1, 1, 1)), result); +}; + +// TODO: Test: +// * extract_version_number: impure, don't know how to get a concrete version number to test against +// * preflight_versions? impure, but m +let () = + Alcotest.( + run( + "Versions", + [ + ( + "compare", + [ + test_case("Versions Equal", `Quick, test_compare_equal), + test_case( + "First Major Greater", + `Quick, + test_compare_first_major_greater, + ), + test_case( + "First Minor Greater", + `Quick, + test_compare_first_minor_greater, + ), + test_case( + "First Point Greater", + `Quick, + test_compare_first_point_greater, + ), + test_case( + "Second Major Greater", + `Quick, + test_compare_second_major_greater, + ), + test_case( + "Second Minor Greater", + `Quick, + test_compare_second_minor_greater, + ), + test_case( + "Second Point Greater", + `Quick, + test_compare_second_point_greater, + ), + ], + ), + ( + "is_new_enough", + [ + test_case("Curr Newer", `Quick, test_ine_cur_newer), + test_case("Curr Older", `Quick, test_ine_cur_older), + test_case("Curr Equal", `Quick, test_ine_cur_equal), + test_case("Error", `Quick, test_ine_error), + ], + ), + ( + "in_direnv", + [ + test_case("true", `Quick, test_in_direnv_true), + test_case("false", `Quick, test_in_direnv_false), + ], + ), + ( + "extract_version_number", + [ + test_case("success", `Quick, test_extract_version_number_success), + ], + ), + ], + ) + ); diff --git a/tests/flake_env_test_watches.re b/tests/flake_env_test_watches.re new file mode 100644 index 0000000..38d4226 --- /dev/null +++ b/tests/flake_env_test_watches.re @@ -0,0 +1,41 @@ +open Lib.Watches; + +let test_get_path_removes_prefix = () => { + let input = `Assoc([ + ("path", `String("aaaaaaaaaaabbbbb")) + ]); + Alcotest.(check(string))("Prefix removed", "bbbbb", get_path(input)) +}; + +let test_get_paths_from_doc = () => { + let input = `Assoc([ + ("path", `String("aaaaaaaaaaabbbbb")), + ("inputs", `Assoc([ + ("foo", `Assoc([ + ("path", `String("aaaaaaaaaaaccccc")), + ("inputs", `Assoc([ + ("bar", `Assoc([ + ("path", `String("aaaaaaaaaaaddddd")), + ("inputs", `Assoc([])) + ])) + ])) + ])) + ])) + ]); + Alcotest.(check(list(string)))("Gathers all inputs", ["bbbbb", "ccccc", "ddddd"], get_paths_from_doc(input, [])) +}; + +let () = + Alcotest.( + run( + "Watches", + [("get_path", + [ + test_case("Removes prefix", `Quick, test_get_path_removes_prefix), + ]), + ("get_paths_from_doc", + [ + test_case("Collects all paths", `Quick, test_get_paths_from_doc), + ]) + ]), +); diff --git a/tests/spit_version.sh b/tests/spit_version.sh new file mode 100755 index 0000000..b2eee26 --- /dev/null +++ b/tests/spit_version.sh @@ -0,0 +1,2 @@ +#!/usr/bin/env sh +echo "1.1.1"; From 5d0923569f1237c639e9a584dae1b55e52148e59 Mon Sep 17 00:00:00 2001 From: Bryan Bennett Date: Mon, 8 Jan 2024 15:29:59 -0500 Subject: [PATCH 06/21] More tests for version.extract_version_number --- Justfile | 5 +++++ lib/flake_env__versions.re | 32 ++++++++++++++++++-------------- tests/dune | 2 +- tests/flake_env_test_versions.re | 14 +++++++++++++- tests/spit_gibberish.sh | 3 +++ 5 files changed, 40 insertions(+), 16 deletions(-) create mode 100755 tests/spit_gibberish.sh diff --git a/Justfile b/Justfile index 9e7ec57..6bcf045 100644 --- a/Justfile +++ b/Justfile @@ -1,5 +1,10 @@ build: @dune build +test: + @dune test -f + @bisect-ppx-report html + @bisect-ppx-report summary --per-file + fmt: @dune build @fmt --auto-promote diff --git a/lib/flake_env__versions.re b/lib/flake_env__versions.re index 0ad683b..1fbcd71 100644 --- a/lib/flake_env__versions.re +++ b/lib/flake_env__versions.re @@ -30,23 +30,27 @@ let compare = (a, b) => { } } -let extract_version_number = (cmd) => { +let extract_version_number = cmd => { switch (Util.run_process(cmd, ["--version"])) { - | (Ok(), stdout) when String.length(stdout) > 0 => { - let substrings = Re.exec(semver_re, stdout); + | (Ok (), stdout) when String.length(stdout) > 0 => + switch (Re.exec(semver_re, stdout)) { + | exception Stdlib.Not_found => + Error( + Printf.sprintf( + "Stdout did not contain a version number for `%s --version`", + cmd, + ), + ) + | substrings => let groups = Re.Group.all(substrings); - if ((groups |> Array.length) == 4) { - Ok({ - major: groups[1] |> int_of_string, - minor: groups[2] |> int_of_string, - point: groups[3] |> int_of_string - }) - } else { - Error(Printf.sprintf("Stdout did not contain a version number for `%s --version`", cmd)) - } + Ok({ + major: groups[1] |> int_of_string, + minor: groups[2] |> int_of_string, + point: groups[3] |> int_of_string, + }); } - | _ => Error(Printf.sprintf("Failed executing '%s'\n", cmd)) - } + | _ => Error(Printf.sprintf("Failed executing '%s'", cmd)) + }; }; let is_new_enough = (cur, needed) => { diff --git a/tests/dune b/tests/dune index 9440d3f..deb6af3 100644 --- a/tests/dune +++ b/tests/dune @@ -3,7 +3,7 @@ flake_env_test_versions flake_env_test_watches flake_env_test_util) - (deps spit_version.sh) + (deps spit_version.sh spit_gibberish.sh) (libraries lib alcotest diff --git a/tests/flake_env_test_versions.re b/tests/flake_env_test_versions.re index 5b06a48..ac5214a 100644 --- a/tests/flake_env_test_versions.re +++ b/tests/flake_env_test_versions.re @@ -103,8 +103,18 @@ let test_extract_version_number_success = () => { check_version("Versions", Ok(Versions.init(1, 1, 1)), result); }; +let test_extract_version_number_no_version = () => { + let result = Versions.extract_version_number("../tests/spit_gibberish.sh"); + check_version("Versions", Error("Stdout did not contain a version number for `../tests/spit_gibberish.sh --version`"), result); +}; + +let test_extract_version_number_nonexistent = () => { + let result = Versions.extract_version_number("nonexistent.sh"); + check_version("Versions", Error("Failed executing 'nonexistent.sh'"), result); +}; + + // TODO: Test: -// * extract_version_number: impure, don't know how to get a concrete version number to test against // * preflight_versions? impure, but m let () = Alcotest.( @@ -167,6 +177,8 @@ let () = "extract_version_number", [ test_case("success", `Quick, test_extract_version_number_success), + test_case("no version number", `Quick, test_extract_version_number_no_version), + test_case("missing binary", `Quick, test_extract_version_number_nonexistent), ], ), ], diff --git a/tests/spit_gibberish.sh b/tests/spit_gibberish.sh new file mode 100755 index 0000000..e767e00 --- /dev/null +++ b/tests/spit_gibberish.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env sh + +echo "sdlfkjdsfweiojlsjslfj.dofiwoksdj/sfowiefjw0"; From fa7b93e3da6796b45fc8c1be3152fd6273924d8d Mon Sep 17 00:00:00 2001 From: Bryan Bennett Date: Mon, 8 Jan 2024 15:40:13 -0500 Subject: [PATCH 07/21] Format with ocamlformat --- bin/dune | 17 +++-- bin/flake_env.re | 84 ++++++++++++----------- lib/dune | 26 +++---- lib/flake_env__util.re | 8 ++- lib/flake_env__versions.re | 59 ++++++++-------- lib/flake_env__watches.re | 50 ++++++++------ lib/lib.re | 113 +++++++++++++++++++------------ tests/dune | 21 +++--- tests/flake_env_test_util.re | 20 ++++-- tests/flake_env_test_versions.re | 27 ++++++-- tests/flake_env_test_watches.re | 79 +++++++++++++-------- 11 files changed, 298 insertions(+), 206 deletions(-) diff --git a/bin/dune b/bin/dune index 9b0e082..186c456 100644 --- a/bin/dune +++ b/bin/dune @@ -1,5 +1,14 @@ (executable - (name flake_env) - (public_name flake_env) - (libraries core core_unix core_unix.filename_unix core_unix.sys_unix ppx_yojson_conv re sha lib) - (preprocess (pps ppx_yojson_conv ppx_jane))) \ No newline at end of file + (name flake_env) + (public_name flake_env) + (libraries + core + core_unix + core_unix.filename_unix + core_unix.sys_unix + ppx_yojson_conv + re + sha + lib) + (preprocess + (pps ppx_yojson_conv ppx_jane))) diff --git a/bin/flake_env.re b/bin/flake_env.re index 2615e9c..4567cb9 100644 --- a/bin/flake_env.re +++ b/bin/flake_env.re @@ -6,50 +6,52 @@ open Lib; let main = () => { let argv = Sys.get_argv(); switch (Util.get_args(argv)) { - | Ok((layout_directory, flake_specifier, other_args)) => { - switch (preflight(layout_directory)) { - | Ok() => { - switch (Lib.Watches.get()) { - | Ok(watches) => { - let paths = Array.map(~f=watch => watch.path, watches); - let hash = Util.hash_files(paths); - let profile = layout_directory ++ "/flake-profile-" ++ hash; - let profile_rc = profile ++ ".rc"; + | Ok((layout_directory, flake_specifier, other_args)) => + switch (preflight(layout_directory)) { + | Ok () => + switch (Lib.Watches.get()) { + | Ok(watches) => + let paths = Array.map(~f=watch => watch.path, watches); + let hash = Util.hash_files(paths); + let profile = layout_directory ++ "/flake-profile-" ++ hash; + let profile_rc = profile ++ ".rc"; - switch ((Sys_unix.is_file(profile_rc), Sys_unix.is_file(profile))) { - | (`Yes, `Yes) => { - let profile_rc_mtime = Unix.stat(profile_rc).st_mtime; - let all_older = Array.map( - ~f=watch => watch.modtime, watches) - |> Array.for_all( - ~f=watch_mtime => watch_mtime <= int_of_float(profile_rc_mtime) - ) - if (all_older) { - print_cur_cache(profile_rc) - } else { - freshen_cache(layout_directory, hash, flake_specifier, other_args) - } - } - | _ => freshen_cache(layout_directory, hash, flake_specifier, other_args) - } - }; - | Error(e) => { - Printf.eprintf("%s\n", e); - exit(1); - } - } - } - | Error(e) => { - Printf.eprintf("%s\n", e); - exit(1); - } - }; - } - | Error() => { - Printf.eprintf("%s <...args>\n", argv[0]); + switch (Sys_unix.is_file(profile_rc), Sys_unix.is_file(profile)) { + | (`Yes, `Yes) => + let profile_rc_mtime = Unix.stat(profile_rc).st_mtime; + let all_older = + Array.map(~f=watch => watch.modtime, watches) + |> Array.for_all(~f=watch_mtime => + watch_mtime <= int_of_float(profile_rc_mtime) + ); + if (all_older) { + print_cur_cache(profile_rc); + } else { + freshen_cache( + layout_directory, + hash, + flake_specifier, + other_args, + ); + }; + | _ => + freshen_cache(layout_directory, hash, flake_specifier, other_args) + }; + | Error(e) => + Printf.eprintf("%s\n", e); + exit(1); + } + | Error(e) => + Printf.eprintf("%s\n", e); exit(1); } - } + | Error () => + Printf.eprintf( + "%s <...args>\n", + argv[0], + ); + exit(1); + }; }; let () = main(); diff --git a/lib/dune b/lib/dune index e268194..168d405 100644 --- a/lib/dune +++ b/lib/dune @@ -1,13 +1,15 @@ (library - (name lib) - (public_name flake_env.lib) - (libraries - core - core_unix - core_unix.filename_unix - core_unix.sys_unix - ppx_yojson_conv - re - sha) - (instrumentation (backend bisect_ppx)) - (preprocess (pps ppx_yojson_conv ppx_jane ))) \ No newline at end of file + (name lib) + (public_name flake_env.lib) + (libraries + core + core_unix + core_unix.filename_unix + core_unix.sys_unix + ppx_yojson_conv + re + sha) + (instrumentation + (backend bisect_ppx)) + (preprocess + (pps ppx_yojson_conv ppx_jane))) diff --git a/lib/flake_env__util.re b/lib/flake_env__util.re index 0e8dfc5..1b5aa7b 100644 --- a/lib/flake_env__util.re +++ b/lib/flake_env__util.re @@ -39,12 +39,16 @@ let hash_files = filenames => { let rec rmrf = path => { switch (Unix.lstat(path).st_kind) { | exception (Unix.Unix_error(_, _, _)) => () - | S_REG | S_LNK => Unix.unlink(path) + | S_REG + | S_LNK => Unix.unlink(path) | S_DIR => Sys_unix.readdir(path) |> Array.iter(~f=name => rmrf(Filename.concat(path, name))); Unix.rmdir(path); - | _ => Printf.eprintf("Unsupported file type (Chr or Block device, FIFO, or Socket)\n") + | _ => + Printf.eprintf( + "Unsupported file type (Chr or Block device, FIFO, or Socket)\n", + ) }; }; diff --git a/lib/flake_env__versions.re b/lib/flake_env__versions.re index 1fbcd71..c36fb04 100644 --- a/lib/flake_env__versions.re +++ b/lib/flake_env__versions.re @@ -3,16 +3,13 @@ module Unix = Core_unix; module Util = Flake_env__util; - type t = { major: int, minor: int, point: int, }; -let init = (major, minor, point) => { - {major, minor, point} -}; +let init = (major, minor, point) => {major, minor, point}; let required_direnv_version = init(2, 21, 3); @@ -23,12 +20,12 @@ let semver_re = Re.compile(Re.Posix.re({|([0-9]+)\.([0-9]+)\.([0-9]+)|})); let compare = (a, b) => { switch (a, b) { | (a, b) when a.major == b.major && a.minor == b.minor && a.point == b.point => 0 - | (a, b) when a.major < b.major => -1 - | (a, b) when a.major == b.major && a.minor < b.minor => -1 - | (a, b) when a.major == b.major && a.minor == b.minor && a.point < b.point => -1 + | (a, b) when a.major < b.major => (-1) + | (a, b) when a.major == b.major && a.minor < b.minor => (-1) + | (a, b) when a.major == b.major && a.minor == b.minor && a.point < b.point => (-1) | _ => 1 - } -} + }; +}; let extract_version_number = cmd => { switch (Util.run_process(cmd, ["--version"])) { @@ -55,31 +52,33 @@ let extract_version_number = cmd => { let is_new_enough = (cur, needed) => { switch (cur) { - | Ok(cur) => { - switch (compare(cur, needed)) { - | x when x < 0 => Ok(false) - | _ => Ok(true) - } - } - | Error(e) => Error(e) - } + | Ok(cur) => + switch (compare(cur, needed)) { + | x when x < 0 => Ok(false) + | _ => Ok(true) + } + | Error(e) => Error(e) + }; }; -let in_direnv = () => switch (Sys.getenv("direnv")) { - | Some(_) => true - | None => false -}; +let in_direnv = () => + switch (Sys.getenv("direnv")) { + | Some(_) => true + | None => false + }; let preflight_versions = () => { - let is_nix_new_enough = is_new_enough(extract_version_number("nix"), required_nix_version); - let is_direnv_new_enough = is_new_enough(extract_version_number("direnv"), required_direnv_version); + let is_nix_new_enough = + is_new_enough(extract_version_number("nix"), required_nix_version); + let is_direnv_new_enough = + is_new_enough(extract_version_number("direnv"), required_direnv_version); switch (in_direnv(), is_direnv_new_enough, is_nix_new_enough) { - | (false, _, _) => Error("Not in direnv!") - | (_, Ok(false), _) => Error("Direnv version is not new enough") - | (_, _, Ok(false)) => Error("Nix version is not new enough") - | (_, Error(e), _) => Error(e) - | (_, _, Error(e)) => Error(e) - | (true, Ok(true), Ok(true)) => Ok() - } + | (false, _, _) => Error("Not in direnv!") + | (_, Ok(false), _) => Error("Direnv version is not new enough") + | (_, _, Ok(false)) => Error("Nix version is not new enough") + | (_, Error(e), _) => Error(e) + | (_, _, Error(e)) => Error(e) + | (true, Ok(true), Ok(true)) => Ok() + }; }; diff --git a/lib/flake_env__watches.re b/lib/flake_env__watches.re index db4cfc7..5f0ee3e 100644 --- a/lib/flake_env__watches.re +++ b/lib/flake_env__watches.re @@ -9,40 +9,52 @@ module Util = Flake_env__util; type watch = { exists: bool, modtime: int, - path: string + path: string, }; [@deriving yojson] -type watches = array; +type watches = array(watch); let get = () => { - let direnv_watch_str = Sys.getenv("DIRENV_WATCHES") |> Option.value_exn(~message="Environment missing DIRENV_WATCHES"); - let proc_info = Unix.create_process(~prog="direnv", ~args=["show_dump", direnv_watch_str]); + let direnv_watch_str = + Sys.getenv("DIRENV_WATCHES") + |> Option.value_exn(~message="Environment missing DIRENV_WATCHES"); + let proc_info = + Unix.create_process( + ~prog="direnv", + ~args=["show_dump", direnv_watch_str], + ); let sub_stdout = Unix.in_channel_of_descr(proc_info.stdout); switch (Unix.waitpid(proc_info.pid)) { - | Ok() => Ok(watches_of_yojson(Yojson.Safe.from_channel(sub_stdout))) - | _ => Error("Failed to parse watches") - } + | Ok () => Ok(watches_of_yojson(Yojson.Safe.from_channel(sub_stdout))) + | _ => Error("Failed to parse watches") + }; }; -let get_path = (doc) => String.drop_prefix(doc |> member("path") |> to_string, 11); +let get_path = doc => + String.drop_prefix(doc |> member("path") |> to_string, 11); let rec get_paths_from_doc = (doc, paths) => { let p = get_path(doc); - let sub_paths = List.concat( - doc |> member("inputs") - |> to_assoc - |> List.map(~f=((_k, v)) => get_paths_from_doc(v, paths))); - List.concat([[p], sub_paths]) + let sub_paths = + List.concat( + doc + |> member("inputs") + |> to_assoc + |> List.map(~f=((_k, v)) => get_paths_from_doc(v, paths)), + ); + List.concat([[p], sub_paths]); }; let get_input_paths = () => { switch (Util.nix(["flake", "archive", "--json", "--no-write-lock-file"])) { - | (Ok(), output) => get_paths_from_doc(Yojson.Safe.from_string(output), []) - | (Error(_), _) => { - Printf.eprintf("Failed to parse output of `nix flake archive --json`. Ignorning flake inputs. \n"); - [] - } - } + | (Ok (), output) => + get_paths_from_doc(Yojson.Safe.from_string(output), []) + | (Error(_), _) => + Printf.eprintf( + "Failed to parse output of `nix flake archive --json`. Ignorning flake inputs. \n", + ); + []; + }; }; diff --git a/lib/lib.re b/lib/lib.re index a9a1f6e..e411503 100644 --- a/lib/lib.re +++ b/lib/lib.re @@ -5,11 +5,11 @@ module Util = Flake_env__util; module Watches = Flake_env__watches; module Versions = Flake_env__versions; -let print_cur_cache = (profile_rc) => { - In_channel.read_all(profile_rc) |> Printf.printf("%s") +let print_cur_cache = profile_rc => { + In_channel.read_all(profile_rc) |> Printf.printf("%s"); }; -let clean_old_gcroots = (layout_dir) => { +let clean_old_gcroots = layout_dir => { Util.rmrf(layout_dir ++ "/flake-inputs/"); Util.rmrf(layout_dir); Unix.mkdir_p(layout_dir ++ "/flake-inputs/"); @@ -17,57 +17,80 @@ let clean_old_gcroots = (layout_dir) => { let add_gcroot = (store_path, symlink) => { switch (Util.nix(["build", "--out-link", symlink, store_path])) { - | (Ok(), _) => Ok() + | (Ok (), _) => Ok() | (err, _) => err - } + }; }; let freshen_cache = (layout_dir, hash, flake_specifier, other_args) => { - clean_old_gcroots(layout_dir); - let tmp_profile = layout_dir ++ "flake-tmp-profile." ++ Core.Pid.to_string(Core_unix.getpid()); + clean_old_gcroots(layout_dir); + let tmp_profile = + layout_dir + ++ "flake-tmp-profile." + ++ Core.Pid.to_string(Core_unix.getpid()); - let pde_args = ["print-dev-env", "--profile", tmp_profile, flake_specifier, ...other_args]; - let (exit_code, stdout_content) = Util.nix(pde_args); + let pde_args = [ + "print-dev-env", + "--profile", + tmp_profile, + flake_specifier, + ...other_args, + ]; + let (exit_code, stdout_content) = Util.nix(pde_args); - let profile = layout_dir ++ "/flake-profile-" ++ hash; - let profile_rc = profile ++ ".rc"; + let profile = layout_dir ++ "/flake-profile-" ++ hash; + let profile_rc = profile ++ ".rc"; - switch (exit_code) { - | Ok() => { - Out_channel.with_file(~f=f=> Out_channel.output_string(f, stdout_content), profile_rc); - switch (add_gcroot(tmp_profile, profile)) { - | Ok() => { - Sys_unix.remove(tmp_profile); - let flake_input_cache_path = layout_dir ++ "/flake-inputs/" - let flake_inputs = Watches.get_input_paths(); - flake_inputs |> List.iter(~f=(inpt) => { - switch (add_gcroot("/nix/store/" ++ inpt, flake_input_cache_path ++ inpt)) { - | Ok() => () - | err => Printf.eprintf("Failed creating flake-input gcroot: %s\n", Core_unix.Exit_or_signal.to_string_hum(err)); - }; - }); - print_cur_cache(profile_rc); - } - | err => { - Printf.eprintf("Failed creating gcroot: %s\n", Core_unix.Exit_or_signal.to_string_hum(err)); - exit(1); - } - }; - } - | err => { - Printf.eprintf("Failed evaluating flake: %s\n", Core_unix.Exit_or_signal.to_string_hum(err)); - exit(1); - } + switch (exit_code) { + | Ok () => + Out_channel.with_file( + ~f=f => Out_channel.output_string(f, stdout_content), + profile_rc, + ); + switch (add_gcroot(tmp_profile, profile)) { + | Ok () => + Sys_unix.remove(tmp_profile); + let flake_input_cache_path = layout_dir ++ "/flake-inputs/"; + let flake_inputs = Watches.get_input_paths(); + flake_inputs + |> List.iter(~f=inpt => { + switch ( + add_gcroot("/nix/store/" ++ inpt, flake_input_cache_path ++ inpt) + ) { + | Ok () => () + | err => + Printf.eprintf( + "Failed creating flake-input gcroot: %s\n", + Core_unix.Exit_or_signal.to_string_hum(err), + ) + } + }); + print_cur_cache(profile_rc); + | err => + Printf.eprintf( + "Failed creating gcroot: %s\n", + Core_unix.Exit_or_signal.to_string_hum(err), + ); + exit(1); }; + | err => + Printf.eprintf( + "Failed evaluating flake: %s\n", + Core_unix.Exit_or_signal.to_string_hum(err), + ); + exit(1); }; +}; -let preflight = (layout_directory) => { - switch (Versions.preflight_versions(), Sys_unix.is_directory(layout_directory)) { +let preflight = layout_directory => { + switch ( + Versions.preflight_versions(), + Sys_unix.is_directory(layout_directory), + ) { | (Ok(_), `Yes) => Ok() - | (Ok(_), _) => { - Unix.mkdir_p(layout_directory); - Ok() - } + | (Ok(_), _) => + Unix.mkdir_p(layout_directory); + Ok(); | (err, _) => err - } -} + }; +}; diff --git a/tests/dune b/tests/dune index deb6af3..dce3e4c 100644 --- a/tests/dune +++ b/tests/dune @@ -1,13 +1,10 @@ (tests - (names - flake_env_test_versions - flake_env_test_watches - flake_env_test_util) - (deps spit_version.sh spit_gibberish.sh) - (libraries - lib - alcotest - core - core_unix - core_unix.sys_unix - core_unix.filename_unix)) \ No newline at end of file + (names flake_env_test_versions flake_env_test_watches flake_env_test_util) + (deps spit_version.sh spit_gibberish.sh) + (libraries + lib + alcotest + core + core_unix + core_unix.sys_unix + core_unix.filename_unix)) diff --git a/tests/flake_env_test_util.re b/tests/flake_env_test_util.re index 6f8e11f..e45ccaf 100644 --- a/tests/flake_env_test_util.re +++ b/tests/flake_env_test_util.re @@ -111,7 +111,11 @@ let test_get_args_just_enough = () => { }; let test_get_args_error = () => { - check_get_args("Errors on too few args", Error(), get_args([|"000", "111"|])) + check_get_args( + "Errors on too few args", + Error(), + get_args([|"000", "111"|]), + ); }; let () = @@ -148,11 +152,15 @@ let () = ), ( "get_args", - [ - test_case("Parses Args", `Quick, test_get_args_simple), - test_case("Parses just enough args", `Quick, test_get_args_just_enough), - test_case("Handles too few args", `Quick, test_get_args_error) - ], + [ + test_case("Parses Args", `Quick, test_get_args_simple), + test_case( + "Parses just enough args", + `Quick, + test_get_args_just_enough, + ), + test_case("Handles too few args", `Quick, test_get_args_error), + ], ), ], ) diff --git a/tests/flake_env_test_versions.re b/tests/flake_env_test_versions.re index ac5214a..b0f7208 100644 --- a/tests/flake_env_test_versions.re +++ b/tests/flake_env_test_versions.re @@ -105,15 +105,24 @@ let test_extract_version_number_success = () => { let test_extract_version_number_no_version = () => { let result = Versions.extract_version_number("../tests/spit_gibberish.sh"); - check_version("Versions", Error("Stdout did not contain a version number for `../tests/spit_gibberish.sh --version`"), result); + check_version( + "Versions", + Error( + "Stdout did not contain a version number for `../tests/spit_gibberish.sh --version`", + ), + result, + ); }; let test_extract_version_number_nonexistent = () => { let result = Versions.extract_version_number("nonexistent.sh"); - check_version("Versions", Error("Failed executing 'nonexistent.sh'"), result); + check_version( + "Versions", + Error("Failed executing 'nonexistent.sh'"), + result, + ); }; - // TODO: Test: // * preflight_versions? impure, but m let () = @@ -177,8 +186,16 @@ let () = "extract_version_number", [ test_case("success", `Quick, test_extract_version_number_success), - test_case("no version number", `Quick, test_extract_version_number_no_version), - test_case("missing binary", `Quick, test_extract_version_number_nonexistent), + test_case( + "no version number", + `Quick, + test_extract_version_number_no_version, + ), + test_case( + "missing binary", + `Quick, + test_extract_version_number_nonexistent, + ), ], ), ], diff --git a/tests/flake_env_test_watches.re b/tests/flake_env_test_watches.re index 38d4226..0a60efb 100644 --- a/tests/flake_env_test_watches.re +++ b/tests/flake_env_test_watches.re @@ -1,41 +1,60 @@ open Lib.Watches; let test_get_path_removes_prefix = () => { - let input = `Assoc([ - ("path", `String("aaaaaaaaaaabbbbb")) - ]); - Alcotest.(check(string))("Prefix removed", "bbbbb", get_path(input)) + let input = `Assoc([("path", `String("aaaaaaaaaaabbbbb"))]); + Alcotest.(check(string))("Prefix removed", "bbbbb", get_path(input)); }; let test_get_paths_from_doc = () => { - let input = `Assoc([ - ("path", `String("aaaaaaaaaaabbbbb")), - ("inputs", `Assoc([ - ("foo", `Assoc([ - ("path", `String("aaaaaaaaaaaccccc")), - ("inputs", `Assoc([ - ("bar", `Assoc([ - ("path", `String("aaaaaaaaaaaddddd")), - ("inputs", `Assoc([])) - ])) - ])) - ])) - ])) - ]); - Alcotest.(check(list(string)))("Gathers all inputs", ["bbbbb", "ccccc", "ddddd"], get_paths_from_doc(input, [])) + let input = + `Assoc([ + ("path", `String("aaaaaaaaaaabbbbb")), + ( + "inputs", + `Assoc([ + ( + "foo", + `Assoc([ + ("path", `String("aaaaaaaaaaaccccc")), + ( + "inputs", + `Assoc([ + ( + "bar", + `Assoc([ + ("path", `String("aaaaaaaaaaaddddd")), + ("inputs", `Assoc([])), + ]), + ), + ]), + ), + ]), + ), + ]), + ), + ]); + Alcotest.(check(list(string)))( + "Gathers all inputs", + ["bbbbb", "ccccc", "ddddd"], + get_paths_from_doc(input, []), + ); }; let () = Alcotest.( - run( - "Watches", - [("get_path", + run( + "Watches", [ - test_case("Removes prefix", `Quick, test_get_path_removes_prefix), - ]), - ("get_paths_from_doc", - [ - test_case("Collects all paths", `Quick, test_get_paths_from_doc), - ]) - ]), -); + ( + "get_path", + [ + test_case("Removes prefix", `Quick, test_get_path_removes_prefix), + ], + ), + ( + "get_paths_from_doc", + [test_case("Collects all paths", `Quick, test_get_paths_from_doc)], + ), + ], + ) + ); From 04337c8c5f9c7b35034eba55221cb62f9cf93675 Mon Sep 17 00:00:00 2001 From: Bryan Bennett Date: Mon, 8 Jan 2024 15:43:53 -0500 Subject: [PATCH 08/21] Add note about justfile in readme --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 9d3ac9d..53a8b09 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,13 @@ watch_file **/*.nix use flake_env . ``` +## Developing + +This repo uses [just](https://just.systems/) to manage simple build jobs. +After activating the development environment in the repo, run `just -l` to list tasks. + +Please remember to format code before issuing patches. + ## Credits This takes huge inspiration (and literal code-chunks) from nix-direnv. From 6e28820659df8c70a279a92da951e5180d0eaf33 Mon Sep 17 00:00:00 2001 From: Bryan Bennett Date: Thu, 11 Jan 2024 08:31:52 -0500 Subject: [PATCH 09/21] Enable testing during nix builds --- default.nix | 13 ++++++++++++- tests/spit_version.sh | 2 +- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/default.nix b/default.nix index 0ef7fe8..22c104a 100644 --- a/default.nix +++ b/default.nix @@ -1,6 +1,9 @@ -{ buildDunePackage +{ alcotest +, bisect_ppx +, buildDunePackage , core , core_unix +, coreutils , findlib , lib , nix-filter @@ -19,21 +22,29 @@ buildDunePackage { include = [ "bin" "lib" + "tests" ./dune-project ./flake.nix ./default.nix ./flake.lock ./flake_env.opam ./direnvrc + ./LICENSE ]; }; duneVersion = "3"; + doCheck = true; postPatch = '' substituteInPlace direnvrc --replace "@flake_env@" "$out/bin/flake_env" + substituteInPlace tests/spit*.sh --replace "/usr/bin/env" "${coreutils}/bin/env" ''; postInstall = '' install -m400 -D direnvrc $out/share/flake_env/direnvrc ''; + checkInputs = [ + alcotest + bisect_ppx + ]; nativeBuildInputs = [ reason ]; diff --git a/tests/spit_version.sh b/tests/spit_version.sh index b2eee26..b8e3877 100755 --- a/tests/spit_version.sh +++ b/tests/spit_version.sh @@ -1,2 +1,2 @@ -#!/usr/bin/env sh +#!/usr/bin/env bash echo "1.1.1"; From 443a10d73851b635f3436d5d06520a80f75f7f1b Mon Sep 17 00:00:00 2001 From: Bryan Bennett Date: Sat, 13 Jan 2024 09:32:09 -0500 Subject: [PATCH 10/21] Add github workflow to proxy tests from sr.ht --- .github/workflows/repo-lockdown.yml | 16 ++++++++++++++++ .github/workflows/test.yml | 14 ++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 .github/workflows/repo-lockdown.yml create mode 100644 .github/workflows/test.yml diff --git a/.github/workflows/repo-lockdown.yml b/.github/workflows/repo-lockdown.yml new file mode 100644 index 0000000..e513c4f --- /dev/null +++ b/.github/workflows/repo-lockdown.yml @@ -0,0 +1,16 @@ +name: "Repo Lockdown" + +on: + pull_request_target: + types: 'opened' + +permissions: + pull-requests: write + +jobs: + action: + runs-on: ubuntu-latest + steps: + - uses: dessant/repo-lockdown@v4 + with: + pr-comment: "This repo does not accept pull requests as it is a mirror. Please instead submit patches to https://sr.ht/~bryan_bennett/flake_env." diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..3d7a1b5 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,14 @@ +name: "Test" +on: + pull_request: + push: +jobs: + tests: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: cachix/install-nix-action@v25 + with: + github_access_token: ${{ secrets.GITHUB_TOKEN }} + - run: nix build + - run: nix flake check --all-systems --keep-going From 44d628b6d913e041e6cfb76a010156afad744102 Mon Sep 17 00:00:00 2001 From: Bryan Bennett Date: Wed, 17 Jan 2024 08:23:08 -0500 Subject: [PATCH 11/21] 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 12/21] 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 13/21] 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 14/21] 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 15/21] 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 16/21] 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 From 606d717276e84caa9807a2efec6c2837018e4a92 Mon Sep 17 00:00:00 2001 From: Bryan Bennett Date: Sat, 23 Mar 2024 07:15:16 -0400 Subject: [PATCH 17/21] Fix error message if failing to hash file --- bin/flake_env.re | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/flake_env.re b/bin/flake_env.re index d2b6dfd..1c147a3 100644 --- a/bin/flake_env.re +++ b/bin/flake_env.re @@ -16,7 +16,7 @@ let main = () => { switch (Util.hash_files(paths)) { | Ok(hsh) => hsh | Error(msg) => - Printf.eprintf("%s\n", e); + Printf.eprintf("%s\n", msg); exit(1); }; From 358ae864fca12cd2b4dfcdc97c6d16214d51aad4 Mon Sep 17 00:00:00 2001 From: Bryan Bennett Date: Sat, 23 Mar 2024 08:16:51 -0400 Subject: [PATCH 18/21] Fix depends specification --- default.nix | 10 ++++++++-- flake.nix | 28 +++++++++++++++------------- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/default.nix b/default.nix index 21e15eb..737856e 100644 --- a/default.nix +++ b/default.nix @@ -30,7 +30,6 @@ buildDunePackage { ./flake.lock ./flake_env.opam ./direnvrc - ./LICENSE ]; }; duneVersion = "3"; @@ -42,9 +41,16 @@ buildDunePackage { postInstall = '' install -m400 -D direnvrc $out/share/flake_env/direnvrc ''; + buildInputs = [ + core + core_unix + 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"; diff --git a/flake.nix b/flake.nix index 4caf913..b61bbff 100644 --- a/flake.nix +++ b/flake.nix @@ -39,19 +39,21 @@ }; 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 - pkgs.ocamlPackages.findlib - pkgs.ocamlPackages.ocaml - pkgs.ocamlPackages.ocaml-lsp - pkgs.ocamlPackages.ocamlformat - pkgs.ocamlPackages.ocamlformat-rpc-lib - ]; + packages = builtins.attrValues { + inherit (pkgs) + just + nil + pre-commit + ; + inherit (pkgs.ocamlPackages) + dune_3 + findlib + ocaml + ocaml-lsp + ocamlformat + ocamlformat-rpc-lib + ; + }; shellHook = config.pre-commit.installationScript; }; pre-commit = { From a2fff9333f555c0dc4983fa6269553ef88d11d5b Mon Sep 17 00:00:00 2001 From: Bryan Bennett Date: Sat, 23 Mar 2024 08:17:20 -0400 Subject: [PATCH 19/21] Strip and pack final binary --- default.nix | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/default.nix b/default.nix index 737856e..7b4b0a7 100644 --- a/default.nix +++ b/default.nix @@ -1,4 +1,5 @@ { alcotest +, bintools , bisect_ppx , buildDunePackage , core @@ -13,6 +14,7 @@ , re , reason , sha +, upx , }: buildDunePackage { @@ -39,6 +41,8 @@ buildDunePackage { substituteInPlace tests/spit*.sh --replace "/usr/bin/env" "${coreutils}/bin/env" ''; postInstall = '' + ${bintools}/bin/strip --strip-unneeded $out/bin/flake_env + ${upx}/bin/upx $out/bin/flake_env install -m400 -D direnvrc $out/share/flake_env/direnvrc ''; buildInputs = [ From b19f5d67c54d98369580c1750c64bc8583e00d7c Mon Sep 17 00:00:00 2001 From: Bryan Bennett Date: Mon, 25 Mar 2024 07:29:29 -0400 Subject: [PATCH 20/21] Add very basic .build.yml --- .build.yml | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 .build.yml diff --git a/.build.yml b/.build.yml new file mode 100644 index 0000000..6118817 --- /dev/null +++ b/.build.yml @@ -0,0 +1,8 @@ +image: nixos/unstable +environment: + NIX_CONFIG: "experimental-features = nix-command flakes" +packages: + - cachix +tasks: + -build: | + nix build . -vL --no-out-link From e513b6e6a6d8621713a791371980972b651ab00c Mon Sep 17 00:00:00 2001 From: Bryan Bennett Date: Mon, 25 Mar 2024 07:30:51 -0400 Subject: [PATCH 21/21] Update flake.lock --- flake.lock | 48 ++++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/flake.lock b/flake.lock index 24f924c..0cfb382 100644 --- a/flake.lock +++ b/flake.lock @@ -23,11 +23,11 @@ ] }, "locked": { - "lastModified": 1701473968, - "narHash": "sha256-YcVE5emp1qQ8ieHUnxt1wCZCC3ZfAS+SRRWZ2TMda7E=", + "lastModified": 1709336216, + "narHash": "sha256-Dt/wOWeW6Sqm11Yh+2+t0dfEWxoMxGBvv3JpIocFl9E=", "owner": "hercules-ci", "repo": "flake-parts", - "rev": "34fed993f1674c8d06d58b37ce1e0fe5eebcb9f5", + "rev": "f7b3c975cf067e56e7cda6cb098ebe3fb4d74ca2", "type": "github" }, "original": { @@ -41,11 +41,11 @@ "systems": "systems" }, "locked": { - "lastModified": 1701680307, - "narHash": "sha256-kAuep2h5ajznlPMD9rnQyffWG8EM/C73lejGofXvdM8=", + "lastModified": 1710146030, + "narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=", "owner": "numtide", "repo": "flake-utils", - "rev": "4022d587cbbfd70fe950c1e2083a02621806a725", + "rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a", "type": "github" }, "original": { @@ -62,11 +62,11 @@ ] }, "locked": { - "lastModified": 1703887061, - "narHash": "sha256-gGPa9qWNc6eCXT/+Z5/zMkyYOuRZqeFZBDbopNZQkuY=", + "lastModified": 1709087332, + "narHash": "sha256-HG2cCnktfHsKV0s4XW83gU3F57gaTljL9KNSuG6bnQs=", "owner": "hercules-ci", "repo": "gitignore.nix", - "rev": "43e1aa1308018f37118e34d3a9cb4f5e75dc11d5", + "rev": "637db329424fd7e46cf4185293b9cc8c88c95394", "type": "github" }, "original": { @@ -77,11 +77,11 @@ }, "nix-filter": { "locked": { - "lastModified": 1701697642, - "narHash": "sha256-L217WytWZHSY8GW9Gx1A64OnNctbuDbfslaTEofXXRw=", + "lastModified": 1710156097, + "narHash": "sha256-1Wvk8UP7PXdf8bCCaEoMnOT1qe5/Duqgj+rL8sRQsSM=", "owner": "numtide", "repo": "nix-filter", - "rev": "c843418ecfd0344ecb85844b082ff5675e02c443", + "rev": "3342559a24e85fc164b295c3444e8a139924675b", "type": "github" }, "original": { @@ -92,11 +92,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1702272962, - "narHash": "sha256-D+zHwkwPc6oYQ4G3A1HuadopqRwUY/JkMwHz1YF7j4Q=", + "lastModified": 1711106783, + "narHash": "sha256-PDwAcHahc6hEimyrgGmFdft75gmLrJOZ0txX7lFqq+I=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "e97b3e4186bcadf0ef1b6be22b8558eab1cdeb5d", + "rev": "a3ed7406349a9335cb4c2a71369b697cecd9d351", "type": "github" }, "original": { @@ -108,11 +108,11 @@ }, "nixpkgs-stable": { "locked": { - "lastModified": 1704874635, - "narHash": "sha256-YWuCrtsty5vVZvu+7BchAxmcYzTMfolSPP5io8+WYCg=", + "lastModified": 1710695816, + "narHash": "sha256-3Eh7fhEID17pv9ZxrPwCLfqXnYP006RKzSs0JptsN84=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "3dc440faeee9e889fe2d1b4d25ad0f430d449356", + "rev": "614b4613980a522ba49f0d194531beddbb7220d3", "type": "github" }, "original": { @@ -124,11 +124,11 @@ }, "nixpkgs_2": { "locked": { - "lastModified": 1704842529, - "narHash": "sha256-OTeQA+F8d/Evad33JMfuXC89VMetQbsU4qcaePchGr4=", + "lastModified": 1710765496, + "narHash": "sha256-p7ryWEeQfMwTB6E0wIUd5V2cFTgq+DRRBz2hYGnJZyA=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "eabe8d3eface69f5bb16c18f8662a702f50c20d5", + "rev": "e367f7a1fb93137af22a3908f00b9a35e2d286a7", "type": "github" }, "original": { @@ -147,11 +147,11 @@ "nixpkgs-stable": "nixpkgs-stable" }, "locked": { - "lastModified": 1705229514, - "narHash": "sha256-itILy0zimR/iyUGq5Dgg0fiW8plRDyxF153LWGsg3Cw=", + "lastModified": 1710923068, + "narHash": "sha256-6hOpUiuxuwpXXc/xfJsBUJeqqgGI+JMJuLo45aG3cKc=", "owner": "cachix", "repo": "pre-commit-hooks.nix", - "rev": "ffa9a5b90b0acfaa03b1533b83eaf5dead819a05", + "rev": "e611897ddfdde3ed3eaac4758635d7177ff78673", "type": "github" }, "original": {