From 122cc1e3ba448fb0adf397b32f2746aee1d72b2d Mon Sep 17 00:00:00 2001 From: figsoda Date: Mon, 27 Feb 2023 22:26:38 -0500 Subject: [PATCH] convert base32 to sri hash with nix-compat instead of nix cli --- Cargo.lock | 86 +++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 ++ src/prefetch.rs | 26 ++++++--------- 3 files changed, 97 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9bdb4ac..8c799ab 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -57,6 +57,15 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "block-buffer" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69cce20737498f97b993470a6e536b8523f0af7892a4f928cceb1ac5e52ebe7e" +dependencies = [ + "generic-array", +] + [[package]] name = "bstr" version = "1.3.0" @@ -172,6 +181,15 @@ dependencies = [ "memchr", ] +[[package]] +name = "cpufeatures" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28d997bd5e24a5928dd43e46dc529867e207907fe0b239c3477d924f7f2ca320" +dependencies = [ + "libc", +] + [[package]] name = "crc32fast" version = "1.3.2" @@ -224,12 +242,38 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "crypto-common" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "data-encoding" +version = "2.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23d8666cb01533c39dde32bcbab8e227b4ed6679b2c925eba05feabea39508fb" + [[package]] name = "difflib" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6184e33543162437515c2e2b48714794e37845ec9851711914eec9d308f6ebe8" +[[package]] +name = "digest" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8168378f4e5023e7218c89c891c0fd8ecdb5e5e4f18cb78f38cf245dd021e76f" +dependencies = [ + "block-buffer", + "crypto-common", +] + [[package]] name = "doc-comment" version = "0.3.3" @@ -321,6 +365,16 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "generic-array" +version = "0.14.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bff49e947297f3312447abdca79f45f4738097cc82b06e72054d2223f601f1b9" +dependencies = [ + "typenum", + "version_check", +] + [[package]] name = "gix-features" version = "0.26.5" @@ -564,6 +618,19 @@ dependencies = [ "adler", ] +[[package]] +name = "nix-compat" +version = "0.1.0" +source = "git+https://code.tvl.fyi/depot.git:/tvix/nix-compat.git#8d4960f273bf627acc4e0f8ff7fb8bec849c67f9" +dependencies = [ + "anyhow", + "data-encoding", + "glob", + "serde", + "sha2", + "thiserror", +] + [[package]] name = "nom8" version = "0.2.0" @@ -605,9 +672,11 @@ dependencies = [ "clap", "clap_complete", "clap_mangen", + "data-encoding", "enum_dispatch", "gix-url", "itertools", + "nix-compat", "nu-glob", "once_cell", "owo-colors", @@ -894,6 +963,17 @@ dependencies = [ "serde", ] +[[package]] +name = "sha2" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82e6b795fe2e3b1e845bafcb27aa35405c4d47cdfc92af5fc8d3002f76cebdc0" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + [[package]] name = "shlex" version = "1.1.0" @@ -1079,6 +1159,12 @@ dependencies = [ "toml_edit", ] +[[package]] +name = "typenum" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba" + [[package]] name = "unicase" version = "2.6.0" diff --git a/Cargo.toml b/Cargo.toml index 000a42d..43b474a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,9 +14,11 @@ categories = ["command-line-utilities"] [dependencies] anyhow = "1.0.69" bstr = "1.3.0" +data-encoding = "2.3.3" enum_dispatch = "0.3.11" gix-url = "0.13.3" itertools = "0.10.5" +nix-compat = { git = "https://code.tvl.fyi/depot.git:/tvix/nix-compat.git" } once_cell = "1.17.1" owo-colors = { version = "3.5.0", features = ["supports-colors"] } rustc-hash = "1.1.0" diff --git a/src/prefetch.rs b/src/prefetch.rs index 4c5ceac..7f21c83 100644 --- a/src/prefetch.rs +++ b/src/prefetch.rs @@ -1,4 +1,6 @@ use anyhow::{anyhow, bail, Result}; +use data_encoding::BASE64; +use nix_compat::nixbase32; use serde::Deserialize; use std::{ @@ -71,6 +73,8 @@ pub fn git_prefetch(git_scheme: bool, url: &str, rev: &str, submodules: bool) -> } pub fn url_prefetch(url: String, unpack: bool) -> Result { + use bstr::ByteSlice; + let mut cmd = Command::new("nix-prefetch-url"); if unpack { cmd.arg("--unpack"); @@ -80,23 +84,11 @@ pub fn url_prefetch(url: String, unpack: bool) -> Result { } cmd.arg(url); - let hash = String::from_utf8(cmd.get_stdout()?)?; - let hash = hash.trim_end(); - - info!("$ nix hash to-sri --experimental-features nix-command --type sha256 {hash}"); - Ok(String::from_utf8( - Command::new("nix") - .arg("hash") - .arg("to-sri") - .arg("--experimental-features") - .arg("nix-command") - .arg("--type") - .arg("sha256") - .arg(hash) - .get_stdout()?, - )? - .trim_end() - .into()) + let hash = cmd.get_stdout()?; + Ok(format!( + "sha256-{}", + BASE64.encode(&nixbase32::decode(hash.trim_end())?), + )) } pub fn fod_prefetch(expr: String) -> Result {