From 543b21112a1e7fdfc9f814c1fc322f77e9aa1b38 Mon Sep 17 00:00:00 2001 From: shouya <526598+shouya@users.noreply.github.com> Date: Thu, 10 Apr 2025 22:12:11 +0900 Subject: [PATCH 1/2] build package for nix --- flake.lock | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ flake.nix | 69 +++++++++++++++++++++++++++++++++++++ 2 files changed, 168 insertions(+) create mode 100644 flake.lock create mode 100644 flake.nix diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..7fafe6e --- /dev/null +++ b/flake.lock @@ -0,0 +1,99 @@ +{ + "nodes": { + "crane": { + "locked": { + "lastModified": 1743908961, + "narHash": "sha256-e1idZdpnnHWuosI3KsBgAgrhMR05T2oqskXCmNzGPq0=", + "owner": "ipetkov", + "repo": "crane", + "rev": "80ceeec0dc94ef967c371dcdc56adb280328f591", + "type": "github" + }, + "original": { + "owner": "ipetkov", + "repo": "crane", + "type": "github" + } + }, + "fenix": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ], + "rust-analyzer-src": [] + }, + "locked": { + "lastModified": 1744231114, + "narHash": "sha256-60gLl2rJFt6SRwqWimsTAeHgfsIE1iV0zChdJFOvx8w=", + "owner": "nix-community", + "repo": "fenix", + "rev": "0ccfe532b1433da8e5a23cd513ff6847e0f6a8c2", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "fenix", + "type": "github" + } + }, + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1731533236, + "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1744157173, + "narHash": "sha256-bWSjxDwq7iVePrhmA7tY2dyMWHuNJo8knkO4y+q4ZkY=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "6a39c6e495eefabc935d8ddf66aa45d85b85fa3f", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "crane": "crane", + "fenix": "fenix", + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..d19a49a --- /dev/null +++ b/flake.nix @@ -0,0 +1,69 @@ +{ + description = "Build a cargo project"; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; + + crane.url = "github:ipetkov/crane"; + + fenix = { + url = "github:nix-community/fenix"; + inputs.nixpkgs.follows = "nixpkgs"; + inputs.rust-analyzer-src.follows = ""; + }; + + flake-utils.url = "github:numtide/flake-utils"; + }; + + outputs = { self, nixpkgs, crane, fenix, flake-utils, ... }: + flake-utils.lib.eachDefaultSystem (system: + let + pkgs = nixpkgs.legacyPackages.${system}; + + inherit (pkgs) lib; + + craneLib = crane.mkLib pkgs; + src = lib.fileset.toSource { + root = ./.; + fileset = lib.fileset.unions [ + (craneLib.fileset.commonCargoSources ./.) + # sql files are included in binary + (lib.fileset.fileFilter (file: file.hasExt "sql") ./.) + ]; + }; + + commonArgs = with pkgs; { + inherit src; + strictDeps = true; + buildInputs = [fuse]; + nativeBuildInputs = [ + pkg-config + openssl + perl # used by openssl-sys build script + ]; + }; + + cargoArtifacts = craneLib.buildDepsOnly commonArgs; + + nightshift = craneLib.buildPackage (commonArgs // { + inherit cargoArtifacts; + }); + in { + checks = { + inherit nightshift; + }; + + packages = { + default = nightshift; + }; + + apps.default = flake-utils.lib.mkApp { + drv = nightshift; + }; + + devShells.default = craneLib.devShell { + checks = self.checks.${system}; + packages = with pkgs; [ rust-analyzer ]; + }; + }); +} From 38ca876169b7c380fffe5288df33299766ac8ee4 Mon Sep 17 00:00:00 2001 From: shouya <526598+shouya@users.noreply.github.com> Date: Thu, 10 Apr 2025 22:28:06 +0900 Subject: [PATCH 2/2] allow for unencrypted databases --- src/database.rs | 6 ++++-- src/main.rs | 17 ++++++++++------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/database.rs b/src/database.rs index 4e225c8..268c75c 100644 --- a/src/database.rs +++ b/src/database.rs @@ -16,9 +16,11 @@ pub struct DatabaseOps { } impl DatabaseOps { - pub fn open(path: &Path, key: String) -> anyhow::Result { + pub fn open(path: &Path, key: Option) -> anyhow::Result { let mut db = rusqlite::Connection::open(path).context("open")?; - set_cipher_key(&db, key)?; + if let Some(key) = key { + set_cipher_key(&db, key)?; + } migrate_database(&mut db)?; Ok(DatabaseOps { db }) } diff --git a/src/main.rs b/src/main.rs index 50cfe7e..fba8b4c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -83,7 +83,7 @@ enum Commands { } #[derive(Debug, clap::Args)] -#[group(required = true, multiple = false)] +#[group(multiple = false)] struct KeyGroup { #[arg(long, help = "Decryption key")] key: Option, @@ -93,21 +93,21 @@ struct KeyGroup { } impl KeyGroup { - fn read_key(self) -> anyhow::Result { + fn read_key(self) -> anyhow::Result> { let key = if let Some(key) = self.key { key } else if let Some(key_file) = self.key_file { let raw_key = fs::read_to_string(key_file)?; raw_key.trim_end().to_owned() } else { - bail!("One of --key or --key-file is required"); + return Ok(None); }; if key.is_empty() { bail!("Key cannot be empty"); } - Ok(key) + Ok(Some(key)) } } @@ -126,7 +126,8 @@ fn main() -> anyhow::Result<()> { compression, key_group, } => { - let db = DatabaseOps::open(&database_path, key_group.read_key()?).context("open db")?; + let key = key_group.read_key()?; + let db = DatabaseOps::open(&database_path, key).context("open db")?; let driver = FuseDriver::new(db, compression.unwrap_or_default(), &mount_path)?; let mount = fuser::spawn_mount2(driver, &mount_path, &[]).context("unable to create mount")?; @@ -150,7 +151,8 @@ fn main() -> anyhow::Result<()> { cmd, args, } => { - let db = DatabaseOps::open(&database_path, key_group.read_key()?).context("open db")?; + let key = key_group.read_key()?; + let db = DatabaseOps::open(&database_path, key).context("open db")?; let driver = FuseDriver::new(db, compression.unwrap_or_default(), &mount_path)?; let mount = fuser::spawn_mount2(driver, &mount_path, &[]).context("unable to create mount")?; defer! { @@ -182,7 +184,8 @@ fn main() -> anyhow::Result<()> { database_path, key_group, } => { - let mut db = DatabaseOps::open(&database_path, key_group.read_key()?).context("open db")?; + let key = key_group.read_key()?; + let mut db = DatabaseOps::open(&database_path, key).context("open db")?; println!("Running VACUUM on database, this may take a few seconds..."); db.vacuum()?; println!("Done!");