|
|
@@ -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 |
66 |
fn init_config() -> Result<()> { |
| 43 |
67 |
let config_dir = get_config_path(); |
| 44 |
68 |
std::fs::create_dir_all(&config_dir)?; |
|
|
@@ -55,27 +79,42 @@ fn init_config() -> Result<()> { |
| 55 |
79 |
let main_template = include_str!("../../templates/main.rs"); |
| 56 |
80 |
std::fs::write(config_dir.join("main.rs"), main_template)?; |
| 57 |
81 |
|
| 58 |
|
- let manifest_dir = env!("CARGO_MANIFEST_DIR"); |
| 59 |
|
- let cargo_toml = format!( |
| 60 |
|
- r#"[package] |
|
82 |
+ let cargo_toml = r#"[package] |
| 61 |
83 |
name = "oxwm-user" |
| 62 |
84 |
version = "0.1.0" |
| 63 |
85 |
edition = "2024" |
| 64 |
86 |
|
| 65 |
87 |
[dependencies] |
| 66 |
|
-oxwm = {{ path = "{}" }} |
|
88 |
+oxwm = { git = "https://github.com/tonybanters/oxwm" } |
| 67 |
89 |
anyhow = "1" |
| 68 |
90 |
|
| 69 |
91 |
[[bin]] |
| 70 |
92 |
name = "oxwm-user" |
| 71 |
93 |
path = "main.rs" |
| 72 |
|
-"#, |
| 73 |
|
- manifest_dir |
| 74 |
|
- ); |
| 75 |
|
- std::fs::write(config_dir.join("Cargo.toml"), cargo_toml)?; |
|
94 |
+"#; |
| 76 |
95 |
|
|
96 |
+ std::fs::write(config_dir.join("Cargo.toml"), cargo_toml)?; |
| 77 |
97 |
std::fs::write(config_dir.join(".gitignore"), "target/\nCargo.lock\n")?; |
| 78 |
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 |
+ |
| 79 |
118 |
println!("✓ Created ~/.config/oxwm/config.rs"); |
| 80 |
119 |
println!("✓ Created ~/.config/oxwm/main.rs"); |
| 81 |
120 |
println!("✓ Edit ~/.config/oxwm/config.rs to customize your setup"); |
|
|
@@ -99,10 +138,28 @@ fn recompile_config() -> Result<()> { |
| 99 |
138 |
|
| 100 |
139 |
println!("Compiling oxwm configuration..."); |
| 101 |
140 |
|
| 102 |
|
- let output = Command::new("cargo") |
| 103 |
|
- .args(&["build", "--release"]) |
| 104 |
|
- .current_dir(&config_dir) |
| 105 |
|
- .output()?; |
|
141 |
+ let build_method = detect_build_method(); |
|
142 |
+ |
|
143 |
+ let output = match build_method { |
|
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 |
+ }; |
| 106 |
163 |
|
| 107 |
164 |
if !output.status.success() { |
| 108 |
165 |
let stderr = String::from_utf8_lossy(&output.stderr); |
|
|
@@ -138,6 +195,12 @@ fn recompile_config() -> Result<()> { |
| 138 |
195 |
Ok(()) |
| 139 |
196 |
} |
| 140 |
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 |
+ |
| 141 |
204 |
fn should_recompile(config: &PathBuf, binary: &PathBuf) -> Result<bool> { |
| 142 |
205 |
if !config.exists() { |
| 143 |
206 |
return Ok(false); |
|
|
@@ -146,7 +209,13 @@ fn should_recompile(config: &PathBuf, binary: &PathBuf) -> Result<bool> { |
| 146 |
209 |
let config_dir = get_config_path(); |
| 147 |
210 |
let binary_time = std::fs::metadata(binary)?.modified()?; |
| 148 |
211 |
|
| 149 |
|
- 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 |
+ ]; |
| 150 |
219 |
|
| 151 |
220 |
for filename in &watch_files { |
| 152 |
221 |
let path = config_dir.join(filename); |
|
|
@@ -191,5 +260,7 @@ fn print_help() { |
| 191 |
260 |
println!(" 4. Start X with 'startx'\n"); |
| 192 |
261 |
println!("CONFIGURATION:"); |
| 193 |
262 |
println!(" Config location: ~/.config/oxwm/config.rs"); |
| 194 |
|
- 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"); |
| 195 |
266 |
} |