diff --git a/fimt.toml b/fimt.toml index 5e6e1d7..b50afbb 100644 --- a/fimt.toml +++ b/fimt.toml @@ -1,3 +1,6 @@ +[direnv] +nix = "flake" + [gitignore] direnv = true rust = true diff --git a/src/cli/direnv.rs b/src/cli/direnv.rs index 73aa9a3..6fb1aa2 100644 --- a/src/cli/direnv.rs +++ b/src/cli/direnv.rs @@ -1,24 +1,34 @@ -use crate::module::direnv::DirenvModuleArgs; +use crate::module::direnv::{DirenvModuleArgs, DirenvNixFunction}; #[derive(Debug, Clone, clap::ValueEnum)] pub enum DirenvCliNixValue { Flake, Shell, } +impl From for DirenvNixFunction { + fn from(value: DirenvCliNixValue) -> Self { + match value { + DirenvCliNixValue::Flake => DirenvNixFunction::Flake, + DirenvCliNixValue::Shell => DirenvNixFunction::Shell, + } + } +} #[derive(Debug, Clone, clap::Args)] pub struct DirenvModuleCliArgs { #[clap(long)] pub nix: Option, #[clap(long)] + pub nix_args: Option, + #[clap(long)] pub nodejs: bool, } impl From for DirenvModuleArgs { fn from(args: DirenvModuleCliArgs) -> Self { DirenvModuleArgs { - nix_shell: matches!(args.nix, Some(DirenvCliNixValue::Shell)), - nix_flake: matches!(args.nix, Some(DirenvCliNixValue::Flake)), + nix: args.nix.map(From::from), + nix_args: args.nix_args, nodejs: args.nodejs, } } diff --git a/src/config/direnv.rs b/src/config/direnv.rs index 24626e2..a4f64fa 100644 --- a/src/config/direnv.rs +++ b/src/config/direnv.rs @@ -1,4 +1,4 @@ -use crate::module::direnv::DirenvModuleArgs; +use crate::module::direnv::{DirenvModuleArgs, DirenvNixFunction}; #[derive(Default, serde::Deserialize, serde::Serialize)] pub struct DirenvModuleConfig { @@ -9,14 +9,18 @@ pub struct DirenvModuleConfig { impl From for DirenvModuleArgs { fn from(cfg: DirenvModuleConfig) -> Self { DirenvModuleArgs { - nix_shell: cfg + nix: cfg .nix - .map(|v| v.to_lowercase() == "shell") - .unwrap_or_default(), - nix_flake: cfg - .nix - .map(|v| v.to_lowercase() == "flake") - .unwrap_or_default(), + .clone() + .and_then(|v| match v.split_whitespace().next() { + Some("flake") => Some(DirenvNixFunction::Flake), + Some("shell") => Some(DirenvNixFunction::Shell), + _ => None, + }), + nix_args: cfg.nix.and_then(|v| match v.split_once(" ") { + Some((_, args)) => Some(String::from(args)), + None => None, + }), nodejs: cfg.nodejs.unwrap_or_default(), } } diff --git a/src/module/direnv.rs b/src/module/direnv.rs index a3a61c6..17a528f 100644 --- a/src/module/direnv.rs +++ b/src/module/direnv.rs @@ -2,29 +2,41 @@ use std::collections::HashMap; use super::Module; -const NIX_SHELL_PART: &str = "\ -# nix -use nix"; - -const NIX_FLAKE_PART: &str = "\ -# nix -use flake"; - const NODEJS_PART: &str = "\ # nodejs layout node"; +pub enum DirenvNixFunction { + Flake, + Shell, +} +impl std::fmt::Display for DirenvNixFunction { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Flake => f.write_str("flake"), + Self::Shell => f.write_str("nix"), + } + } +} + pub struct DirenvModuleArgs { - pub nix_shell: bool, - pub nix_flake: bool, + pub nix: Option, + pub nix_args: Option, pub nodejs: bool, } fn make_gitignore_content(args: DirenvModuleArgs) -> String { [ - args.nix_shell.then_some(NIX_SHELL_PART), - args.nix_flake.then_some(NIX_FLAKE_PART), - args.nodejs.then_some(NODEJS_PART), + args.nix.map(|function| { + format!( + "# nix\nuse {} {}", + function, + args.nix_args.unwrap_or_default() + ) + .trim_end() + .to_owned() + }), + args.nodejs.then_some(NODEJS_PART.to_string()), ] .iter() .flatten()