///| /// pname from a store-path name: everything before the first "-". fn pname_of(name : String) -> String { let chars = name.to_array() for i in 0..= '0' && n <= '9' { return name[0:i].to_owned() } } } name } ///| async fn resolve_target( cfg : Config, target : String, ) -> (@store.StorePath, String) raise @store.NixError { if target.has_prefix("/") { let sp = @store.StorePath::parse(target) return (sp, pname_of(sp.name)) } // "-" base name given directly? if target.length() > 33 && target[32:33].to_owned() == "-" { let sp = @store.StorePath::parse_base(target) return (sp, pname_of(sp.name)) } let url = cfg.channel.job_url(target, cfg.system) eprintln("resolving '\{target}' via \{url}") let body = match @cache.http_get(url, allow_404=true, accept_json=true) { Some(b) => b None => raise Network( "attribute '\{target}' has no successful \{cfg.system} build in \{cfg.channel.name} (Hydra \{cfg.channel.project}/\{cfg.channel.jobset})", ) } let text = @utf8.decode(body) catch { _ => raise Parse("Hydra response is not UTF-8") } let (path, _) = @resolve.parse_hydra_build(text) (@store.StorePath::parse(path), target) } ///| async fn cmd_resolve( cfg : Config, target : String, ) -> Unit raise @store.NixError { let (sp, _) = resolve_target(cfg, target) println(sp.logical()) } ///| async fn fetch_closure( cfg : Config, sp : @store.StorePath, ) -> Array[@narinfo.NarInfo] raise @store.NixError { @closure.compute([sp.base_name()], async fn( h, ) -> @narinfo.NarInfo? raise @store.NixError { cfg.cache.fetch_narinfo(h) }) } ///| async fn cmd_closure( cfg : Config, target : String, ) -> Unit raise @store.NixError { let (sp, _) = resolve_target(cfg, target) let c = fetch_closure(cfg, sp) let mut total = 0L for ni in c { total += ni.nar_size println("\{ni.store_path} \{ni.nar_size} bytes") } println("\{c.length()} paths, \{total} bytes (unpacked NAR size)") } ///| async fn install_one( cfg : Config, ni : @narinfo.NarInfo, ) -> Bool raise @store.NixError { let sp = @store.StorePath::parse(ni.store_path) if @installer.lexists("\{cfg.store_dir}/\{sp.base_name()}") { return false } eprintln( "fetching \{sp.base_name()} (\{ni.file_size} bytes, \{ni.compression})", ) let raw = cfg.cache.fetch_nar(ni) let node = @nar.parse(raw) @installer.install_node(cfg.store_dir, sp.base_name(), node) } ///| async fn cmd_install( cfg : Config, target : String, no_profile : Bool, dry_run : Bool, ) -> Unit raise @store.NixError { let (sp, name) = resolve_target(cfg, target) eprintln("target: \{sp.logical()}") let c = fetch_closure(cfg, sp) eprintln("closure: \{c.length()} paths (all signatures verified)") if dry_run { for ni in c { println(ni.store_path) } return } if cfg.store_dir != @store.default_store_dir { eprintln( "note: installing into \{cfg.store_dir}; binaries hardcode /nix/store, use `mnix run` (see README)", ) } let mut fresh = 0 for ni in c { if install_one(cfg, ni) { fresh += 1 } } eprintln( "installed \{fresh} new paths, \{c.length() - fresh} already present", ) if !no_profile { let installed = "\{cfg.store_dir}/\{sp.base_name()}" let e = cfg.profile.add(name, installed) println("\{e.name}: \{e.path}") let bl = e.bins.join(", ") println("linked: \{bl} -> \{cfg.profile.dir}/bin") } else { println("\{cfg.store_dir}/\{sp.base_name()}") } } ///| async fn cmd_list(cfg : Config) -> Unit raise @store.NixError { let es = cfg.profile.list() if es.length() == 0 { println("(profile \{cfg.profile.dir} is empty)") } for e in es { let bl = e.bins.join(", ") println("\{e.name}\t\{e.path}\t[\{bl}]") } } ///| async fn cmd_remove(cfg : Config, name : String) -> Unit raise @store.NixError { let e = cfg.profile.remove(name) println( "removed \{e.name} (store path \{e.path} kept; no garbage collector yet)", ) } ///| fn cmd_env(cfg : Config) -> Unit { println("export PATH=\"\{cfg.profile.dir}/bin:$PATH\"") } ///| async fn cmd_search(cfg : Config, term : String) -> Unit { let cache_dir = "\{cfg.root}/cache" let cache_file = "\{cache_dir}/\{cfg.channel.name}-store-paths.txt" let text = if @installer.lexists(cache_file) { eprintln("using cached \{cache_file} (delete it to refresh)") @utf8.decode(@fs.read_file(cache_file).binary()) } else { let url = "\{cfg.channel.releases_url}/store-paths.xz" eprintln("downloading \{url}") let raw = match @cache.http_get(url) { Some(b) => b None => raise @store.Network("unreachable") } eprintln("decompressing \{raw.length()} bytes of xz") let data = @xz.decompress(raw) catch { _ => raise @store.Verify("store-paths.xz failed to decompress") } let text = @utf8.decode(data) @fs.mkdir(cache_dir, recursive=true, allow_exist=true) @fs.write_file(cache_file, text, create_mode=CreateOrTruncate) text } let paths = @resolve.parse_store_paths(text) let res = @resolve.search(paths, term) let mut n = 0 for r in res { if n >= 50 { println("... (\{res.length() - 50} more)") break } println("\{r.0}\t(\{r.1} path(s) across platforms)") n += 1 } if res.length() == 0 { println("no store path names contain '\{term}'") } } ///| /// Shell used to make `` appear at /nix/store inside a private /// user+mount namespace (no root needed): build a tmpfs root that mirrors /// the real root via recursive bind mounts, bind the store at /nix/store, /// chroot into it. let run_script : String = #|set -e #|STORE="$1"; shift #|R=$(mktemp -d "${TMPDIR:-/tmp}/mnix-root.XXXXXX") #|mount -t tmpfs none "$R" #|for d in /*; do #| n=${d#/} #| [ "$n" = nix ] && continue #| if [ -L "$d" ]; then ln -s "$(readlink "$d")" "$R/$n"; continue; fi #| [ -d "$d" ] || continue #| mkdir "$R/$n" #| mount --rbind "$d" "$R/$n" #|done #|mkdir -p "$R/nix/store" #|mount --bind "$STORE" "$R/nix/store" #|exec chroot "$R" /bin/sh -c 'cd "$0" && exec "$@"' "$PWD" "$@" ///| async fn cmd_run( cfg : Config, argv : Array[String], ) -> Int32 raise @store.NixError { if argv.length() == 0 { raise Parse("run: no command given") } let path = match @env.get_env_var("PATH") { Some(p) => p None => "" } let extra : Map[String, String] = { "PATH": "\{cfg.profile.dir}/bin:\{path}" } // The OS spawns argv[0] using *our* PATH, not the child's: resolve // profile commands to their symlink explicitly. let candidate = "\{cfg.profile.dir}/bin/\{argv[0]}" if !argv[0].contains("/") && @installer.lexists(candidate) { argv[0] = candidate } try { if cfg.store_dir == @store.default_store_dir { @process.run(argv[0], argv[1:], extra_env=extra) } else { let args = ["-rm", "sh", "-c", run_script, "mnix-run", cfg.store_dir] for a in argv { args.push(a) } @process.run("unshare", args, extra_env=extra) } } catch { e => raise Io("running \{argv[0]}: \{e}") } }