Updated compiler to detect os...
Browse files
+81 −7 src/bin/main.rs
| @@ -39,6 +39,30 @@ fn main() -> Result<()> { | |||
| 39 | } | 39 | } |
| 40 | } | 40 | } |
| 41 | 41 | ||
| 42 | +#[derive(Debug)] | ||
| 43 | +enum BuildMethod { | ||
| 44 | + NixFlake, | ||
| 45 | + NixBuild, | ||
| 46 | + Cargo, | ||
| 47 | +} | ||
| 48 | + | ||
| 49 | +fn detect_build_method() -> BuildMethod { | ||
| 50 | + let config_dir = get_config_path(); | ||
| 51 | + | ||
| 52 | + if config_dir.join("flake.nix").exists() { | ||
| 53 | + println!("Detected flake.nix, will use nix flake for recompilation"); | ||
| 54 | + return BuildMethod::NixFlake; | ||
| 55 | + } | ||
| 56 | + | ||
| 57 | + if config_dir.join("default.nix").exists() || config_dir.join("shell.nix").exists() { | ||
| 58 | + println!("Detected nix files, will use nix-shell for recompilation"); | ||
| 59 | + return BuildMethod::NixBuild; | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + println!("Will use cargo for recompilation"); | ||
| 63 | + BuildMethod::Cargo | ||
| 64 | +} | ||
| 65 | + | ||
| 42 | fn init_config() -> Result<()> { | 66 | fn init_config() -> Result<()> { |
| 43 | let config_dir = get_config_path(); | 67 | let config_dir = get_config_path(); |
| 44 | std::fs::create_dir_all(&config_dir)?; | 68 | std::fs::create_dir_all(&config_dir)?; |
| @@ -70,9 +94,27 @@ path = "main.rs" | |||
| 70 | "#; | 94 | "#; |
| 71 | 95 | ||
| 72 | std::fs::write(config_dir.join("Cargo.toml"), cargo_toml)?; | 96 | std::fs::write(config_dir.join("Cargo.toml"), cargo_toml)?; |
| 73 | - | ||
| 74 | std::fs::write(config_dir.join(".gitignore"), "target/\nCargo.lock\n")?; | 97 | std::fs::write(config_dir.join(".gitignore"), "target/\nCargo.lock\n")?; |
| 75 | 98 | ||
| 99 | + if is_nixos() { | ||
| 100 | + let shell_nix = r#"{ pkgs ? import <nixpkgs> {} }: | ||
| 101 | +pkgs.mkShell { | ||
| 102 | + buildInputs = with pkgs; [ | ||
| 103 | + rustc | ||
| 104 | + cargo | ||
| 105 | + pkg-config | ||
| 106 | + xorg.libX11 | ||
| 107 | + xorg.libXft | ||
| 108 | + xorg.libXrender | ||
| 109 | + freetype | ||
| 110 | + fontconfig | ||
| 111 | + ]; | ||
| 112 | +} | ||
| 113 | +"#; | ||
| 114 | + std::fs::write(config_dir.join("shell.nix"), shell_nix)?; | ||
| 115 | + println!("✓ Created shell.nix for NixOS"); | ||
| 116 | + } | ||
| 117 | + | ||
| 76 | println!("✓ Created ~/.config/oxwm/config.rs"); | 118 | println!("✓ Created ~/.config/oxwm/config.rs"); |
| 77 | println!("✓ Created ~/.config/oxwm/main.rs"); | 119 | println!("✓ Created ~/.config/oxwm/main.rs"); |
| 78 | println!("✓ Edit ~/.config/oxwm/config.rs to customize your setup"); | 120 | println!("✓ Edit ~/.config/oxwm/config.rs to customize your setup"); |
| @@ -96,10 +138,28 @@ fn recompile_config() -> Result<()> { | |||
| 96 | 138 | ||
| 97 | println!("Compiling oxwm configuration..."); | 139 | println!("Compiling oxwm configuration..."); |
| 98 | 140 | ||
| 99 | - let output = Command::new("cargo") | 141 | + let build_method = detect_build_method(); |
| 100 | - .args(&["build", "--release"]) | 142 | + |
| 101 | - .current_dir(&config_dir) | 143 | + let output = match build_method { |
| 102 | - .output()?; | 144 | + BuildMethod::NixFlake => { |
| 145 | + println!("Using nix flake build..."); | ||
| 146 | + Command::new("nix") | ||
| 147 | + .args(&["build", ".#", "--no-link"]) | ||
| 148 | + .current_dir(&config_dir) | ||
| 149 | + .output()? | ||
| 150 | + } | ||
| 151 | + BuildMethod::NixBuild => { | ||
| 152 | + println!("Using nix-shell..."); | ||
| 153 | + Command::new("nix-shell") | ||
| 154 | + .args(&["--run", "cargo build --release"]) | ||
| 155 | + .current_dir(&config_dir) | ||
| 156 | + .output()? | ||
| 157 | + } | ||
| 158 | + BuildMethod::Cargo => Command::new("cargo") | ||
| 159 | + .args(&["build", "--release"]) | ||
| 160 | + .current_dir(&config_dir) | ||
| 161 | + .output()?, | ||
| 162 | + }; | ||
| 103 | 163 | ||
| 104 | if !output.status.success() { | 164 | if !output.status.success() { |
| 105 | let stderr = String::from_utf8_lossy(&output.stderr); | 165 | let stderr = String::from_utf8_lossy(&output.stderr); |
| @@ -135,6 +195,12 @@ fn recompile_config() -> Result<()> { | |||
| 135 | Ok(()) | 195 | Ok(()) |
| 136 | } | 196 | } |
| 137 | 197 | ||
| 198 | +fn is_nixos() -> bool { | ||
| 199 | + std::path::Path::new("/etc/NIXOS").exists() | ||
| 200 | + || std::path::Path::new("/run/current-system/nixos-version").exists() | ||
| 201 | + || std::env::var("NIX_PATH").is_ok() | ||
| 202 | +} | ||
| 203 | + | ||
| 138 | fn should_recompile(config: &PathBuf, binary: &PathBuf) -> Result<bool> { | 204 | fn should_recompile(config: &PathBuf, binary: &PathBuf) -> Result<bool> { |
| 139 | if !config.exists() { | 205 | if !config.exists() { |
| 140 | return Ok(false); | 206 | return Ok(false); |
| @@ -143,7 +209,13 @@ fn should_recompile(config: &PathBuf, binary: &PathBuf) -> Result<bool> { | |||
| 143 | let config_dir = get_config_path(); | 209 | let config_dir = get_config_path(); |
| 144 | let binary_time = std::fs::metadata(binary)?.modified()?; | 210 | let binary_time = std::fs::metadata(binary)?.modified()?; |
| 145 | 211 | ||
| 146 | - let watch_files = ["config.rs", "main.rs", "Cargo.toml"]; | 212 | + let watch_files = [ |
| 213 | + "config.rs", | ||
| 214 | + "main.rs", | ||
| 215 | + "Cargo.toml", | ||
| 216 | + "flake.nix", | ||
| 217 | + "shell.nix", | ||
| 218 | + ]; | ||
| 147 | 219 | ||
| 148 | for filename in &watch_files { | 220 | for filename in &watch_files { |
| 149 | let path = config_dir.join(filename); | 221 | let path = config_dir.join(filename); |
| @@ -188,5 +260,7 @@ fn print_help() { | |||
| 188 | println!(" 4. Start X with 'startx'\n"); | 260 | println!(" 4. Start X with 'startx'\n"); |
| 189 | println!("CONFIGURATION:"); | 261 | println!("CONFIGURATION:"); |
| 190 | println!(" Config location: ~/.config/oxwm/config.rs"); | 262 | println!(" Config location: ~/.config/oxwm/config.rs"); |
| 191 | - println!(" Reload hotkey: Mod+Shift+R (auto-recompiles if needed)"); | 263 | + println!(" Reload hotkey: Mod+Shift+R (auto-recompiles if needed)\n"); |
| 264 | + println!("ADVANCED:"); | ||
| 265 | + println!(" Create flake.nix or shell.nix in ~/.config/oxwm to use nix builds"); | ||
| 192 | } | 266 | } |