feat: added nix flake #5

Merged
cafkafk merged 8 commits from dev into main 2023-06-18 20:02:36 +02:00
12 changed files with 186 additions and 29 deletions
Showing only changes of commit 551c406591 - Show all commits

7
Cargo.lock generated
View file

@ -196,6 +196,7 @@ dependencies = [
"clap_mangen", "clap_mangen",
"log", "log",
"pretty_env_logger", "pretty_env_logger",
"relative-path",
"serde", "serde",
"serde_yaml", "serde_yaml",
] ]
@ -359,6 +360,12 @@ version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "436b050e76ed2903236f032a59761c1eb99e1b0aead2c257922771dab1fc8c78" checksum = "436b050e76ed2903236f032a59761c1eb99e1b0aead2c257922771dab1fc8c78"
[[package]]
name = "relative-path"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4bf2521270932c3c7bed1a59151222bd7643c79310f2916f01925e1e16255698"
[[package]] [[package]]
name = "roff" name = "roff"
version = "0.2.1" version = "0.2.1"

View file

@ -16,6 +16,7 @@ clap = { version = "4.0.22", features = ["derive"] }
log = "0.4" log = "0.4"
#env_logger = "0.9" #env_logger = "0.9"
pretty_env_logger = "0.4" pretty_env_logger = "0.4"
relative-path = "1.8.0"
[build-dependencies] [build-dependencies]
clap = { version = "4.3.2", features = ["derive", "cargo", "env", "help"] } clap = { version = "4.3.2", features = ["derive", "cargo", "env", "help"] }

View file

@ -134,7 +134,8 @@
"crane": "crane", "crane": "crane",
"fenix": "fenix", "fenix": "fenix",
"flake-utils": "flake-utils_2", "flake-utils": "flake-utils_2",
"nixpkgs": "nixpkgs" "nixpkgs": "nixpkgs",
"test-directory": "test-directory"
} }
}, },
"rust-overlay": { "rust-overlay": {
@ -191,6 +192,18 @@
"repo": "default", "repo": "default",
"type": "github" "type": "github"
} }
},
"test-directory": {
"flake": false,
"locked": {
"narHash": "sha256-d6xi4mKdjkX2JFicDIv5niSzpyI0m/Hnm8GGAIU04kY=",
"type": "file",
"url": "file:///home/ces/org/src/git/gg"
},
"original": {
"type": "file",
"url": "file:///home/ces/org/src/git/gg"
}
} }
}, },
"root": "root", "root": "root",

View file

@ -21,9 +21,14 @@
url = "github:rustsec/advisory-db"; url = "github:rustsec/advisory-db";
flake = false; flake = false;
}; };
test-directory = {
url = "file:///home/ces/org/src/git/gg";
flake = false;
};
}; };
outputs = { self, nixpkgs, crane, fenix, flake-utils, advisory-db, ... }: outputs = { self, nixpkgs, crane, fenix, flake-utils, advisory-db, test-directory, ... }:
flake-utils.lib.eachDefaultSystem (system: flake-utils.lib.eachDefaultSystem (system:
let let
pkgs = import nixpkgs { pkgs = import nixpkgs {
@ -33,13 +38,35 @@
inherit (pkgs) lib; inherit (pkgs) lib;
craneLib = crane.lib.${system}; craneLib = crane.lib.${system};
src = craneLib.cleanCargoSource (craneLib.path ./.);
#src = craneLib.cleanCargoSource (craneLib.path ./.);
# When filtering sources, we want to allow assets other than .rs files
src = lib.cleanSourceWith {
src = ./.; # The original, unfiltered source
filter = path: type:
(lib.hasSuffix "\.yaml" path) ||
# (lib.hasSuffix "\.scss" path) ||
# Example of a folder for images, icons, etc
#(lib.hasInfix "test" path) ||
# Default filter from crane (allow .rs files)
(craneLib.filterCargoSources path type)
;
};
# src = pkgs.lib.cleanSourceWith {
# src = craneLib.path ./.; # original, unfiltered source
# filter = path: type:
# (builtins.match ".*yaml" path != null) # include JSONC files
# || (craneLib.filterCargoSources path type);
# };
# Common arguments can be set here to avoid repeating them later # Common arguments can be set here to avoid repeating them later
commonArgs = { commonArgs = {
inherit src; inherit src;
buildInputs = [ buildInputs = [
# test-directory
# Add additional build inputs here # Add additional build inputs here
] ++ lib.optionals pkgs.stdenv.isDarwin [ ] ++ lib.optionals pkgs.stdenv.isDarwin [
# Additional darwin specific inputs can be set here # Additional darwin specific inputs can be set here
@ -48,6 +75,7 @@
# Additional environment variables can be set directly # Additional environment variables can be set directly
# MY_CUSTOM_VAR = "some value"; # MY_CUSTOM_VAR = "some value";
RUST_BACKTRACE = 1;
}; };
craneLibLLvmTools = craneLib.overrideToolchain craneLibLLvmTools = craneLib.overrideToolchain
@ -65,6 +93,7 @@
# artifacts from above. # artifacts from above.
my-crate = craneLib.buildPackage (commonArgs // { my-crate = craneLib.buildPackage (commonArgs // {
inherit cargoArtifacts; inherit cargoArtifacts;
# doCheck = false; # NOTE remove me
}); });
in in
{ {
@ -100,6 +129,7 @@
# Run tests with cargo-nextest # Run tests with cargo-nextest
# Consider setting `doCheck = false` on `my-crate` if you do not want # Consider setting `doCheck = false` on `my-crate` if you do not want
# the tests to run twice # the tests to run twice
# NOTE enable this part
my-crate-nextest = craneLib.cargoNextest (commonArgs // { my-crate-nextest = craneLib.cargoNextest (commonArgs // {
inherit cargoArtifacts; inherit cargoArtifacts;
partitions = 1; partitions = 1;
@ -108,9 +138,9 @@
} // lib.optionalAttrs (system == "x86_64-linux") { } // lib.optionalAttrs (system == "x86_64-linux") {
# NB: cargo-tarpaulin only supports x86_64 systems # NB: cargo-tarpaulin only supports x86_64 systems
# Check code coverage (note: this will not upload coverage anywhere) # Check code coverage (note: this will not upload coverage anywhere)
my-crate-coverage = craneLib.cargoTarpaulin (commonArgs // { # my-crate-coverage = craneLib.cargoTarpaulin (commonArgs // {
inherit cargoArtifacts; # inherit cargoArtifacts;
}); # });
}; };
packages = { packages = {

View file

@ -99,7 +99,7 @@ impl GitRepo {
.arg(&self.url) .arg(&self.url)
.arg(&self.name) .arg(&self.name)
.status() .status()
.expect("failed to add"); .unwrap_or_else(|_| panic!("git repo failed to add: {:?}", &self,));
info!("{out}"); info!("{out}");
} else { } else {
info!("{} has clone set to false, not cloned", &self.name); info!("{} has clone set to false, not cloned", &self.name);
@ -111,7 +111,7 @@ impl GitRepo {
.current_dir(format!("{}{}", &self.path, &self.name)) .current_dir(format!("{}{}", &self.path, &self.name))
.arg("pull") .arg("pull")
.status() .status()
.expect("failed to pull"); .unwrap_or_else(|_| panic!("git repo failed to pull: {:?}", &self,));
info!("{out}"); info!("{out}");
} }
/// Adds all files in the repository. /// Adds all files in the repository.
@ -167,9 +167,16 @@ impl Config {
/// Reads the configuration toml from a path. /// Reads the configuration toml from a path.
pub fn new(path: &String) -> Self { pub fn new(path: &String) -> Self {
debug!("initializing new Config struct"); debug!("initializing new Config struct");
let yaml = fs::read_to_string(path).expect("Should have been able to read the file"); let yaml = fs::read_to_string(path).unwrap_or_else(|_| {
panic!("Should have been able to read the file: path -> {:?}", path,)
});
debug!("deserialized yaml from config file"); debug!("deserialized yaml from config file");
serde_yaml::from_str(&yaml).expect("Should have been able to deserialize yaml config") serde_yaml::from_str(&yaml).unwrap_or_else(|_| {
panic!(
"Should have been able to deserialize yaml config: path -> {:?}",
path,
)
})
} }
/// Tries to pull all repositories, skips if fail. /// Tries to pull all repositories, skips if fail.
pub fn pull_all(&self) { pub fn pull_all(&self) {

View file

@ -72,9 +72,11 @@ fn main() {
mod config { mod config {
use crate::*; use crate::*;
use git::GitRepo; use git::GitRepo;
use relative_path::RelativePath;
use std::env::current_dir;
use std::fs::File; use std::fs::File;
use std::io::prelude::*; use std::io::prelude::*;
use utils::dir::current_dir; //use utils::dir::current_dir;
#[test] #[test]
fn init_config() { fn init_config() {
let _config = Config { let _config = Config {
@ -102,30 +104,43 @@ mod config {
} }
#[test] #[test]
fn read_config_populate() { fn read_config_populate() {
const CONFIG_FILE: &str = "/tst/config.yaml"; let _config = Config::new(&RelativePath::new("./src/test/config.yaml").to_string());
let config_path = current_dir() + CONFIG_FILE;
let _config = Config::new(&config_path);
} }
#[test] #[test]
fn write_config() { fn write_config() {
const CONFIG_FILE: &str = "/tst/config.yaml"; let root = current_dir().unwrap();
const CONFIG_TEST: &str = "/tst/test.yaml"; let config = Config::new(
let config_path = current_dir() + CONFIG_FILE; &RelativePath::new("./src/test/config.yaml")
let config = Config::new(&config_path); .to_logical_path(&root)
.into_os_string()
.into_string()
.unwrap(),
);
let test_path = current_dir() + CONFIG_TEST; let mut test_file = File::create(
let mut file = File::create(&test_path).unwrap(); RelativePath::new("./src/test/test.yaml")
.to_logical_path(&root)
.into_os_string()
.into_string()
.unwrap(),
)
.expect("failed to create test file");
let contents = serde_yaml::to_string(&config).unwrap(); let contents = serde_yaml::to_string(&config).unwrap();
file.write_all(contents.as_bytes()).unwrap(); test_file.write_all(contents.as_bytes()).unwrap();
let test_config = Config::new(&test_path); let test_config = Config::new(&RelativePath::new("./src/test/test.yaml").to_string());
assert_eq!(config, test_config); assert_eq!(config, test_config);
} }
#[test] #[test]
fn read_and_verify_config() { fn read_and_verify_config() {
const CONFIG_FILE: &str = "/tst/config.yaml"; let root = current_dir().unwrap();
let config_path = current_dir() + CONFIG_FILE; let config = Config::new(
let config = Config::new(&config_path); &RelativePath::new("./src/test/config.yaml")
.to_logical_path(root)
.into_os_string()
.into_string()
.unwrap(),
);
// FIXME This is unnecessarily terse // FIXME This is unnecessarily terse
#[allow(clippy::bool_assert_comparison)] #[allow(clippy::bool_assert_comparison)]
{ {
@ -163,17 +178,24 @@ mod config {
} }
} }
/* Unable to test with networking inside flake
#[cfg(test)] #[cfg(test)]
mod repo_actions { mod repo_actions {
use crate::*; use crate::*;
use git::GitRepo; use git::GitRepo;
use relative_path::RelativePath;
use std::env::current_dir;
use std::process::Command; use std::process::Command;
use utils::dir::current_dir;
#[test] #[test]
#[allow(clippy::redundant_clone)] #[allow(clippy::redundant_clone)]
fn test_repo_actions() { fn test_repo_actions() {
let test_repo_name: String = "test".to_string(); let test_repo_name: String = "test".to_string();
let test_repo_dir: String = (current_dir() + "/tst/").to_string(); let root = current_dir().unwrap();
let test_repo_dir: String = RelativePath::new("./src/test")
.to_logical_path(&root)
.into_os_string()
.into_string()
.unwrap();
let test_repo_url: String = "git@github.com:cafkafk/test.git".to_string(); let test_repo_url: String = "git@github.com:cafkafk/test.git".to_string();
println!("{}", test_repo_dir); println!("{}", test_repo_dir);
let mut config = Config { let mut config = Config {
@ -187,8 +209,10 @@ mod repo_actions {
clone: true, clone: true,
}; };
config.repos.push(repo); config.repos.push(repo);
config.clone_all(); // BUG FIXME can't do this in flake
config.pull_all(); // should have a good alternative
// config.clone_all();
// config.pull_all();
for r in config.repos.iter() { for r in config.repos.iter() {
Command::new("touch") Command::new("touch")
.current_dir(&(r.path.to_owned() + &r.name)) .current_dir(&(r.path.to_owned() + &r.name))
@ -200,3 +224,4 @@ mod repo_actions {
config.commit_all_msg(&"test".to_string()); config.commit_all_msg(&"test".to_string());
} }
} }
*/

1
src/test/test Submodule

@ -0,0 +1 @@
Subproject commit 318edb997ddb88b3dd255b70c8b73e1ced7b74f9

24
src/test/test.yaml Normal file
View file

@ -0,0 +1,24 @@
repos:
- name: gg
path: /home/ces/.dots/
url: git@github.com:cafkafk/gg.git
clone: true
- name: li
path: /home/ces/org/src/git/
url: git@github.com:cafkafk/li.git
clone: true
- name: qmk_firmware
path: /home/ces/org/src/git/
url: git@github.com:cafkafk/qmk_firmware.git
clone: true
- name: starship
path: /home/ces/org/src/git/
url: https://github.com/starship/starship.git
clone: true
links:
- name: gg
rx: /home/ces/.config/gg
tx: /home/ces/.dots/gg
- name: starship
rx: /home/ces/.config/starship.toml
tx: /home/ces/.dots/starship.toml

24
test/config.yaml Normal file
View file

@ -0,0 +1,24 @@
repos:
- name: gg
path: /home/ces/.dots/
url: git@github.com:cafkafk/gg.git
clone: true
- name: li
path: /home/ces/org/src/git/
url: git@github.com:cafkafk/li.git
clone: true
- name: qmk_firmware
path: /home/ces/org/src/git/
url: git@github.com:cafkafk/qmk_firmware.git
clone: true
- name: starship
path: /home/ces/org/src/git/
url: https://github.com/starship/starship.git
clone: true
links:
- name: gg
rx: /home/ces/.config/gg
tx: /home/ces/.dots/gg
- name: starship
rx: /home/ces/.config/starship.toml
tx: /home/ces/.dots/starship.toml

1
test/test Submodule

@ -0,0 +1 @@
Subproject commit 318edb997ddb88b3dd255b70c8b73e1ced7b74f9

24
test/test.yaml Normal file
View file

@ -0,0 +1,24 @@
repos:
- name: gg
path: /home/ces/.dots/
url: git@github.com:cafkafk/gg.git
clone: true
- name: li
path: /home/ces/org/src/git/
url: git@github.com:cafkafk/li.git
clone: true
- name: qmk_firmware
path: /home/ces/org/src/git/
url: git@github.com:cafkafk/qmk_firmware.git
clone: true
- name: starship
path: /home/ces/org/src/git/
url: https://github.com/starship/starship.git
clone: true
links:
- name: gg
rx: /home/ces/.config/gg
tx: /home/ces/.dots/gg
- name: starship
rx: /home/ces/.config/starship.toml
tx: /home/ces/.dots/starship.toml