module Util = Flake_env__util; module Watches = Flake_env__watches; module Versions = Flake_env__versions; let read_file = f_path => In_channel.with_open_bin(f_path, In_channel.input_all); let write_file = (f_path, content) => Out_channel.with_open_bin(f_path, c => Out_channel.output_string(c, content) ); let print_cur_cache = profile_rc => { read_file(profile_rc) |> Printf.printf("%s"); }; let clean_old_gcroots = layout_dir => { FileUtil.rm([layout_dir], ~recurse=true); FileUtil.mkdir(~parent=true, layout_dir ++ "/flake-inputs/"); }; let add_gcroot = (store_path, symlink) => { switch (Util.nix(["build", "--out-link", symlink, store_path])) { | (WEXITED(0), _) => Ok() | (_, _) => Error("Failed to run `nix build`!") }; }; let freshen_cache = (layout_dir, hash, flake_specifier, other_args) => { let () = clean_old_gcroots(layout_dir); let tmp_profile = layout_dir ++ "flake-tmp-profile." ++ string_of_int(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) { | WEXITED(0) => write_file(profile_rc, stdout_content); switch (add_gcroot(tmp_profile, profile)) { | Ok () => let () = FileUtil.rm([tmp_profile]); let flake_input_cache_path = layout_dir ++ "/flake-inputs/"; let flake_inputs = Watches.get_input_paths(); flake_inputs |> List.iter(inpt => { switch ( add_gcroot("/nix/store/" ++ inpt, flake_input_cache_path ++ inpt) ) { | Ok () => () | Error(err) => Printf.eprintf("Failed creating flake-input gcroot: %s\n", err) } }); print_cur_cache(profile_rc); | Error(err) => Printf.eprintf("Failed creating gcroot: %s\n", err); exit(1); }; | _ => Printf.eprintf("Failed evaluating flake\n"); exit(1); }; }; let preflight = layout_directory => { switch (Versions.preflight_versions(), Util.is_directory(layout_directory)) { | (Ok(_), true) => Ok() | (Ok(_), false) => FileUtil.mkdir(~parent=true, layout_directory); Ok(); | (err, _) => err }; };