From 8cdd3cdfb3afb76b01c8dc013d95e6336e4dfb82 Mon Sep 17 00:00:00 2001 From: sternenseemann <0rpkxez4ksa01gb3typccl0i@systemli.org> Date: Sat, 13 Feb 2021 23:41:29 +0100 Subject: pkgs/profpatsch/nman: use pkgs.sternenseemann.temp for TempDir temp is essentially the old TempDir code from nman, but using libc's mkdtemp(3) directly instead of mktemp(1) and a slightly better API. --- pkgs/profpatsch/default.nix | 3 ++- pkgs/profpatsch/nman/default.nix | 8 ++++++- pkgs/profpatsch/nman/nman.rs | 48 +++++----------------------------------- 3 files changed, 15 insertions(+), 44 deletions(-) (limited to 'pkgs') diff --git a/pkgs/profpatsch/default.nix b/pkgs/profpatsch/default.nix index 0fd31aeb..ff90a05f 100644 --- a/pkgs/profpatsch/default.nix +++ b/pkgs/profpatsch/default.nix @@ -1,4 +1,4 @@ -{ stdenv, lib, pkgs }: +{ stdenv, lib, pkgs, sternenseemann }: let inherit (pkgs) callPackage; @@ -129,6 +129,7 @@ in rec { nix-http-serve = callPackage ./nix-http-serve {}; nman = callPackage ./nman { inherit writeRustSimpleBin; + inherit (sternenseemann) temp; }; sfttime = callPackage ./sfttime {}; show-qr-code = callPackage ./show-qr-code {}; diff --git a/pkgs/profpatsch/nman/default.nix b/pkgs/profpatsch/nman/default.nix index 9d74d63b..db081e5a 100644 --- a/pkgs/profpatsch/nman/default.nix +++ b/pkgs/profpatsch/nman/default.nix @@ -1,8 +1,14 @@ -{ lib, writeRustSimpleBin }: +{ lib +, writeRustSimpleBin +, temp +}: writeRustSimpleBin "nman" { meta = { license = lib.licenses.gpl3Only; description = "Open man page in a temporary nix-shell"; }; + dependencies = [ + temp + ]; } ./nman.rs diff --git a/pkgs/profpatsch/nman/nman.rs b/pkgs/profpatsch/nman/nman.rs index c289e6ff..6e7ecf5d 100644 --- a/pkgs/profpatsch/nman/nman.rs +++ b/pkgs/profpatsch/nman/nman.rs @@ -1,46 +1,10 @@ +extern crate temp; + use std::ffi::{OsStr, OsString}; -use std::io::{Error, ErrorKind, self, Write}; use std::os::unix::ffi::OsStrExt; -use std::path::{Path, PathBuf}; -use std::process::{Command,ExitStatus}; - -struct TempDir { - inner: Vec, -} - -impl AsRef for TempDir { - fn as_ref(&self) -> &Path { - OsStr::from_bytes(&self.inner[..]).as_ref() - } -} - -impl Drop for TempDir { - fn drop(&mut self) { - std::fs::remove_dir_all(self.as_ref()); - std::fs::remove_dir(self.as_ref()); - } -} - -fn mktemp(suffix: &str) -> std::io::Result { - let mut mktemp = Command::new("mktemp") - .arg("-d") - .arg("--suffix") - .arg(suffix) - .output()?; - - if mktemp.status.success() { - // remove trailing newline - if mktemp.stdout.ends_with(b"\n") { - mktemp.stdout.pop(); - } - - Ok(TempDir { - inner: mktemp.stdout - }) - } else { - Err(Error::new(ErrorKind::Other, "mktemp exited with a non-zero status")) - } -} +use std::path::PathBuf; +use std::process::Command; +use temp::TempDir; enum NmanError<'a> { NoTempDir, @@ -194,7 +158,7 @@ fn build_man_page<'a>(drv: DrvWithOutput, section: &str, page: &str, tempdir: &T } fn open_man_page<'a>(attr: &'a str, section: &'a str, page: &'a str) -> Result<(), NmanError<'a>> { - let tmpdir = mktemp("-nman").map_err(|_| NmanError::NoTempDir)?; + let tmpdir = TempDir::new("nman").map_err(|_| NmanError::NoTempDir)?; let expr = format!("with (import {{}}); builtins.map (o: {}.\"${{o}}\") {}.outputs", attr, attr); let inst = Command::new("nix-instantiate") .arg("-E") -- cgit 1.4.1