Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

command-not-found: rewrite in Rust #88517

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions nixos/modules/programs/command-not-found/command-not-found.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,9 @@ with lib;

let
cfg = config.programs.command-not-found;
commandNotFound = pkgs.substituteAll {
name = "command-not-found";
dir = "bin";
src = ./command-not-found.pl;
isExecutable = true;
inherit (pkgs) perl;
commandNotFound = pkgs.callPackage ./. {
inherit (cfg) dbPath;
perlFlags = concatStrings (map (path: "-I ${path}/${pkgs.perl.libPrefix} ")
[ pkgs.perlPackages.DBI pkgs.perlPackages.DBDSQLite pkgs.perlPackages.StringShellQuote ]);
};

in

{
Expand Down Expand Up @@ -91,5 +83,4 @@ in

environment.systemPackages = [ commandNotFound ];
};

}
51 changes: 0 additions & 51 deletions nixos/modules/programs/command-not-found/command-not-found.pl

This file was deleted.

18 changes: 18 additions & 0 deletions nixos/modules/programs/command-not-found/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{ stdenv, rustPlatform, pkg-config, sqlite
, dbPath ? "/nix/var/nix/profiles/per-user/root/channels/nixos/programs.sqlite" }:

rustPlatform.buildRustPackage {
name = "command-not-found";
src = ./rust;

DB_PATH = dbPath;
NIX_SYSTEM = stdenv.system;

postInstall = ''
strip $out/bin/command-not-found
'';

buildInputs = [ sqlite ];
nativeBuildInputs = [ pkg-config ];
cargoSha256 = "13q61bb4b1q40g424pbssyp3ln79q1a33vmyz9s9wlqnac34cibd";
Mic92 marked this conversation as resolved.
Show resolved Hide resolved
}
131 changes: 131 additions & 0 deletions nixos/modules/programs/command-not-found/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions nixos/modules/programs/command-not-found/rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "command-not-found"
version = "0.1.0"
edition = "2018"

[dependencies]
rusqlite = "0.*.*"

[profile.release]
lto = true
52 changes: 52 additions & 0 deletions nixos/modules/programs/command-not-found/rust/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use rusqlite::{params, Connection, Result};
use std::env;
use std::process::exit;

const NIX_SYSTEM: &str = env!("NIX_SYSTEM");
const DB_PATH: &str = env!("DB_PATH");

fn query_packages(system: &str, program: &str) -> Result<Vec<String>> {
Connection::open(DB_PATH)?
.prepare("select package from Programs where system = ? and name = ?;")?
.query_map(params![system, program], |row| row.get("package"))?
.collect::<Result<Vec<String>>>()
}

fn run_app() -> i32 {
let args: Vec<_> = env::args().collect();
if args.len() < 2 {
eprintln!("USAGE: {} PROGRAMNAME", args[0]);
return 1;
}
let program = &args[1];
let system = env::var("NIX_SYSTEM").unwrap_or_else(|_| NIX_SYSTEM.to_string());
let packages = match query_packages(&system, program) {
Ok(packages) => packages,
Err(err) => {
eprintln!("Failed to query package database: {}", err);
return 1;
}
};
if packages.is_empty() {
eprintln!("{}: command not found", program);
} else {
let advice = if packages.len() > 1 {
"It is provided by several packages. You can install it by typing one of the following commands:"
} else {
"You can install it by typing:"
};
eprintln!(
"The program '{}' is currently not installed. {}",
program, advice
);
for pkg in packages {
eprintln!(" nix-env -iA nixos.{}", pkg);
}
Mic92 marked this conversation as resolved.
Show resolved Hide resolved
}

127
}

fn main() {
exit(run_app());
}