oxwm

https://git.tonybtw.com/oxwm.git git://git.tonybtw.com/oxwm.git

Updated compiler to detect os...

Commit
1fb52fe678834f7507e5d810be0af2a85532a6cd
Parent
25cc6d7
Author
tonybanters <tonybanters@gmail.com>
Date
2025-10-10 20:33:44

Diff

diff --git a/src/bin/main.rs b/src/bin/main.rs
index 6d5d4b4..11b1b83 100644
--- a/src/bin/main.rs
+++ b/src/bin/main.rs
@@ -39,6 +39,30 @@ fn main() -> Result<()> {
     }
 }
 
+#[derive(Debug)]
+enum BuildMethod {
+    NixFlake,
+    NixBuild,
+    Cargo,
+}
+
+fn detect_build_method() -> BuildMethod {
+    let config_dir = get_config_path();
+
+    if config_dir.join("flake.nix").exists() {
+        println!("Detected flake.nix, will use nix flake for recompilation");
+        return BuildMethod::NixFlake;
+    }
+
+    if config_dir.join("default.nix").exists() || config_dir.join("shell.nix").exists() {
+        println!("Detected nix files, will use nix-shell for recompilation");
+        return BuildMethod::NixBuild;
+    }
+
+    println!("Will use cargo for recompilation");
+    BuildMethod::Cargo
+}
+
 fn init_config() -> Result<()> {
     let config_dir = get_config_path();
     std::fs::create_dir_all(&config_dir)?;
@@ -70,9 +94,27 @@ path = "main.rs"
 "#;
 
     std::fs::write(config_dir.join("Cargo.toml"), cargo_toml)?;
-
     std::fs::write(config_dir.join(".gitignore"), "target/\nCargo.lock\n")?;
 
+    if is_nixos() {
+        let shell_nix = r#"{ pkgs ? import <nixpkgs> {} }:
+pkgs.mkShell {
+  buildInputs = with pkgs; [
+    rustc
+    cargo
+    pkg-config
+    xorg.libX11
+    xorg.libXft
+    xorg.libXrender
+    freetype
+    fontconfig
+  ];
+}
+"#;
+        std::fs::write(config_dir.join("shell.nix"), shell_nix)?;
+        println!("✓ Created shell.nix for NixOS");
+    }
+
     println!("✓ Created ~/.config/oxwm/config.rs");
     println!("✓ Created ~/.config/oxwm/main.rs");
     println!("✓ Edit ~/.config/oxwm/config.rs to customize your setup");
@@ -96,10 +138,28 @@ fn recompile_config() -> Result<()> {
 
     println!("Compiling oxwm configuration...");
 
-    let output = Command::new("cargo")
-        .args(&["build", "--release"])
-        .current_dir(&config_dir)
-        .output()?;
+    let build_method = detect_build_method();
+
+    let output = match build_method {
+        BuildMethod::NixFlake => {
+            println!("Using nix flake build...");
+            Command::new("nix")
+                .args(&["build", ".#", "--no-link"])
+                .current_dir(&config_dir)
+                .output()?
+        }
+        BuildMethod::NixBuild => {
+            println!("Using nix-shell...");
+            Command::new("nix-shell")
+                .args(&["--run", "cargo build --release"])
+                .current_dir(&config_dir)
+                .output()?
+        }
+        BuildMethod::Cargo => Command::new("cargo")
+            .args(&["build", "--release"])
+            .current_dir(&config_dir)
+            .output()?,
+    };
 
     if !output.status.success() {
         let stderr = String::from_utf8_lossy(&output.stderr);
@@ -135,6 +195,12 @@ fn recompile_config() -> Result<()> {
     Ok(())
 }
 
+fn is_nixos() -> bool {
+    std::path::Path::new("/etc/NIXOS").exists()
+        || std::path::Path::new("/run/current-system/nixos-version").exists()
+        || std::env::var("NIX_PATH").is_ok()
+}
+
 fn should_recompile(config: &PathBuf, binary: &PathBuf) -> Result<bool> {
     if !config.exists() {
         return Ok(false);
@@ -143,7 +209,13 @@ fn should_recompile(config: &PathBuf, binary: &PathBuf) -> Result<bool> {
     let config_dir = get_config_path();
     let binary_time = std::fs::metadata(binary)?.modified()?;
 
-    let watch_files = ["config.rs", "main.rs", "Cargo.toml"];
+    let watch_files = [
+        "config.rs",
+        "main.rs",
+        "Cargo.toml",
+        "flake.nix",
+        "shell.nix",
+    ];
 
     for filename in &watch_files {
         let path = config_dir.join(filename);
@@ -188,5 +260,7 @@ fn print_help() {
     println!("    4. Start X with 'startx'\n");
     println!("CONFIGURATION:");
     println!("    Config location: ~/.config/oxwm/config.rs");
-    println!("    Reload hotkey:   Mod+Shift+R (auto-recompiles if needed)");
+    println!("    Reload hotkey:   Mod+Shift+R (auto-recompiles if needed)\n");
+    println!("ADVANCED:");
+    println!("    Create flake.nix or shell.nix in ~/.config/oxwm to use nix builds");
 }