diff --git a/pkgs/by-name/ch/chroot-realpath/package.nix b/pkgs/by-name/ch/chroot-realpath/package.nix new file mode 100644 index 000000000000..af537cb48e45 --- /dev/null +++ b/pkgs/by-name/ch/chroot-realpath/package.nix @@ -0,0 +1,21 @@ +{ + lib, + rustPlatform, +}: + +let + cargo = lib.importTOML ./src/Cargo.toml; +in +rustPlatform.buildRustPackage { + pname = cargo.package.name; + version = cargo.package.version; + + src = ./src; + + cargoLock.lockFile = ./src/Cargo.lock; + + meta = { + description = "Output a path's realpath within a chroot."; + maintainers = [ lib.maintainers.elvishjerricco ]; + }; +} diff --git a/pkgs/by-name/ch/chroot-realpath/src/Cargo.lock b/pkgs/by-name/ch/chroot-realpath/src/Cargo.lock new file mode 100644 index 000000000000..d00f13df1877 --- /dev/null +++ b/pkgs/by-name/ch/chroot-realpath/src/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "chroot-realpath" +version = "0.1.0" diff --git a/pkgs/by-name/ch/chroot-realpath/src/Cargo.toml b/pkgs/by-name/ch/chroot-realpath/src/Cargo.toml new file mode 100644 index 000000000000..52348e7596e9 --- /dev/null +++ b/pkgs/by-name/ch/chroot-realpath/src/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "chroot-realpath" +version = "0.1.0" +edition = "2021" + +[dependencies] + +[profile.release] +opt-level = "z" diff --git a/pkgs/by-name/ch/chroot-realpath/src/src/main.rs b/pkgs/by-name/ch/chroot-realpath/src/src/main.rs new file mode 100644 index 000000000000..4e7873bf0798 --- /dev/null +++ b/pkgs/by-name/ch/chroot-realpath/src/src/main.rs @@ -0,0 +1,24 @@ +use std::env; +use std::io::{stdout, Error, ErrorKind, Write}; +use std::os::unix::ffi::OsStrExt; +use std::os::unix::fs; + +fn main() -> std::io::Result<()> { + let args: Vec = env::args().collect(); + + if args.len() != 3 { + return Err(Error::new( + ErrorKind::InvalidInput, + format!("Usage: {} ", args[0]), + )); + } + + fs::chroot(&args[1])?; + std::env::set_current_dir("/")?; + + let path = std::fs::canonicalize(&args[2])?; + + stdout().write_all(path.into_os_string().as_bytes())?; + + Ok(()) +}