open Core; module Unix = Core_unix; /*** Run a process [name] with args [args], returning (exit_code, stdout text) */ let run_process = (name, args) => { 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); }; /*** Run nix with some fixed arguments, appending a user provided set of arguments. */ let nix = args => run_process( "nix", ["--extra-experimental-features", "\"nix-command flakes\" ", ...args], ); let hash_files = filenames => { /*** Hash all entries in [filenames] which represent existing files. Returns Some(hex-string) or None if no filenames are found. */ let ctx = Sha1.init(); let files_to_hash = filenames |> Array.filter(~f=f => switch (Sys_unix.file_exists(f)) { | `Yes => true | _ => Printf.eprintf( "Cannot find file %s (cwd: %s)\n", f, Core_unix.getcwd(), ); false; } ); 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 => { /*** Remove [path] recursively */ 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() }; };