tony / oxwm

Merge remote-tracking branch 'origin/master' Browse files
tonybtw committed 2025-10-11 parents bddfa10 c7cfa1f commit ac5ee9ef3fde1e5c1455c28a23bf8655130b0f78
Showing 3 changed files with 134 additions and 71 deletions. UnifiedSplit
+23 −10 default.nix
@@ -1,12 +1,13 @@
1 -{ lib 1 +{
2 -, rustPlatform 2 + lib,
3 -, pkg-config 3 + rustPlatform,
4 -, xorg 4 + pkg-config,
5 -, freetype 5 + xorg,
6 -, fontconfig 6 + freetype,
7 + fontconfig,
8 + makeDesktopItem,
7 }: 9 }:
8 - 10 +rustPlatform.buildRustPackage (finalAttrs: {
9 -rustPlatform.buildRustPackage {
10 pname = "oxwm"; 11 pname = "oxwm";
11 version = "0.1.0"; 12 version = "0.1.0";
12 13
@@ -28,12 +29,24 @@ rustPlatform.buildRustPackage {
28 fontconfig 29 fontconfig
29 ]; 30 ];
30 31
32 + postInstall = let
33 + oxwmDesktopItem = makeDesktopItem rec {
34 + name = finalAttrs.pname;
35 + exec = name;
36 + desktopName = name;
37 + comment = finalAttrs.meta.description;
38 + };
39 + in ''
40 + install -Dt $out/share/xsessions ${oxwmDesktopItem}/share/applications/oxwm.desktop
41 + '';
42 +
43 + passthru.providedSessions = ["oxwm"];
44 +
31 meta = with lib; { 45 meta = with lib; {
32 description = "A dynamic window manager written in Rust, inspired by dwm"; 46 description = "A dynamic window manager written in Rust, inspired by dwm";
33 homepage = "https://github.com/tonybanters/oxwm"; 47 homepage = "https://github.com/tonybanters/oxwm";
34 license = licenses.gpl3; 48 license = licenses.gpl3;
35 - maintainers = [ ];
36 platforms = platforms.linux; 49 platforms = platforms.linux;
37 mainProgram = "oxwm"; 50 mainProgram = "oxwm";
38 }; 51 };
39 -} 52 +})
+26 −47 flake.nix
@@ -7,19 +7,16 @@
7 self, 7 self,
8 nixpkgs, 8 nixpkgs,
9 }: let 9 }: let
10 - systems = ["x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin"]; 10 + systems = ["x86_64-linux" "aarch64-linux"];
11 - forAllSystems = nixpkgs.lib.genAttrs systems; 11 +
12 + forAllSystems = fn: nixpkgs.lib.genAttrs systems (system: fn nixpkgs.legacyPackages.${system});
12 in { 13 in {
13 - packages = forAllSystems (system: let 14 + packages = forAllSystems (pkgs: rec {
14 - pkgs = import nixpkgs {inherit system;};
15 - in {
16 default = pkgs.callPackage ./default.nix {}; 15 default = pkgs.callPackage ./default.nix {};
17 - oxwm = self.packages.${system}.default; 16 + oxwm = default;
18 }); 17 });
19 18
20 - devShells = forAllSystems (system: let 19 + devShells = forAllSystems (pkgs: {
21 - pkgs = import nixpkgs {inherit system;};
22 - in {
23 default = pkgs.mkShell { 20 default = pkgs.mkShell {
24 buildInputs = [ 21 buildInputs = [
25 pkgs.rustc 22 pkgs.rustc
@@ -40,51 +37,33 @@
40 }; 37 };
41 }); 38 });
42 39
43 - formatter = forAllSystems (system: (import nixpkgs {inherit system;}).alejandra); 40 + formatter = forAllSystems (pkgs: pkgs.alejandra);
44 41
45 nixosModules.default = { 42 nixosModules.default = {
46 config, 43 config,
47 lib, 44 lib,
48 pkgs, 45 pkgs,
49 ... 46 ...
50 - }: 47 + }: let
51 - with lib; let 48 + inherit (lib) mkEnableOption mkOption mkIf types;
52 - cfg = config.services.xserver.windowManager.oxwm; 49 + cfg = config.services.xserver.windowManager.oxwm;
53 - oxwmDesktopItem = pkgs.writeTextFile { 50 + in {
54 - name = "oxwm.desktop"; 51 + options.services.xserver.windowManager.oxwm = {
55 - destination = "/share/xsessions/oxwm.desktop"; 52 + enable = mkEnableOption "oxwm window manager";
56 - text = '' 53 + package = mkOption {
57 - [Desktop Entry] 54 + type = types.package;
58 - Name=OXWM 55 + default = self.packages.${pkgs.system}.default;
59 - Comment=A dynamic window manager written in Rust 56 + description = "The oxwm package to use";
60 - Exec=${cfg.package}/bin/oxwm
61 - Type=Application
62 - DesktopNames=OXWM
63 - '';
64 - passthru.providedSessions = ["oxwm"];
65 - };
66 - in {
67 - options.services.xserver.windowManager.oxwm = {
68 - enable = mkEnableOption "oxwm window manager";
69 - package = mkOption {
70 - type = types.package;
71 - default = self.packages.${pkgs.system}.default;
72 - description = "The oxwm package to use";
73 - };
74 - };
75 - config = mkIf cfg.enable {
76 - services.xserver.windowManager.session = [
77 - {
78 - name = "oxwm";
79 - start = ''
80 - ${cfg.package}/bin/oxwm &
81 - waitPID=$!
82 - '';
83 - }
84 - ];
85 - services.displayManager.sessionPackages = [oxwmDesktopItem];
86 - environment.systemPackages = [cfg.package];
87 }; 57 };
88 }; 58 };
59 +
60 + config = mkIf cfg.enable {
61 + services.displayManager.sessionPackages = [cfg.package];
62 +
63 + environment.systemPackages = [
64 + cfg.package
65 + ];
66 + };
67 + };
89 }; 68 };
90 } 69 }
+85 −14 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)?;
@@ -55,27 +79,42 @@ fn init_config() -> Result<()> {
55 let main_template = include_str!("../../templates/main.rs"); 79 let main_template = include_str!("../../templates/main.rs");
56 std::fs::write(config_dir.join("main.rs"), main_template)?; 80 std::fs::write(config_dir.join("main.rs"), main_template)?;
57 81
58 - let manifest_dir = env!("CARGO_MANIFEST_DIR"); 82 + let cargo_toml = r#"[package]
59 - let cargo_toml = format!(
60 - r#"[package]
61 name = "oxwm-user" 83 name = "oxwm-user"
62 version = "0.1.0" 84 version = "0.1.0"
63 edition = "2024" 85 edition = "2024"
64 86
65 [dependencies] 87 [dependencies]
66 -oxwm = {{ path = "{}" }} 88 +oxwm = { git = "https://github.com/tonybanters/oxwm" }
67 anyhow = "1" 89 anyhow = "1"
68 90
69 [[bin]] 91 [[bin]]
70 name = "oxwm-user" 92 name = "oxwm-user"
71 path = "main.rs" 93 path = "main.rs"
72 -"#, 94 +"#;
73 - manifest_dir
74 - );
75 - std::fs::write(config_dir.join("Cargo.toml"), cargo_toml)?;
76 95
96 + std::fs::write(config_dir.join("Cargo.toml"), cargo_toml)?;
77 std::fs::write(config_dir.join(".gitignore"), "target/\nCargo.lock\n")?; 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 println!("✓ Created ~/.config/oxwm/config.rs"); 118 println!("✓ Created ~/.config/oxwm/config.rs");
80 println!("✓ Created ~/.config/oxwm/main.rs"); 119 println!("✓ Created ~/.config/oxwm/main.rs");
81 println!("✓ Edit ~/.config/oxwm/config.rs to customize your setup"); 120 println!("✓ Edit ~/.config/oxwm/config.rs to customize your setup");
@@ -99,10 +138,28 @@ fn recompile_config() -> Result<()> {
99 138
100 println!("Compiling oxwm configuration..."); 139 println!("Compiling oxwm configuration...");
101 140
102 - let output = Command::new("cargo") 141 + let build_method = detect_build_method();
103 - .args(&["build", "--release"]) 142 +
104 - .current_dir(&config_dir) 143 + let output = match build_method {
105 - .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 + };
106 163
107 if !output.status.success() { 164 if !output.status.success() {
108 let stderr = String::from_utf8_lossy(&output.stderr); 165 let stderr = String::from_utf8_lossy(&output.stderr);
@@ -138,6 +195,12 @@ fn recompile_config() -> Result<()> {
138 Ok(()) 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 fn should_recompile(config: &PathBuf, binary: &PathBuf) -> Result<bool> { 204 fn should_recompile(config: &PathBuf, binary: &PathBuf) -> Result<bool> {
142 if !config.exists() { 205 if !config.exists() {
143 return Ok(false); 206 return Ok(false);
@@ -146,7 +209,13 @@ fn should_recompile(config: &PathBuf, binary: &PathBuf) -> Result<bool> {
146 let config_dir = get_config_path(); 209 let config_dir = get_config_path();
147 let binary_time = std::fs::metadata(binary)?.modified()?; 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 for filename in &watch_files { 220 for filename in &watch_files {
152 let path = config_dir.join(filename); 221 let path = config_dir.join(filename);
@@ -191,5 +260,7 @@ fn print_help() {
191 println!(" 4. Start X with 'startx'\n"); 260 println!(" 4. Start X with 'startx'\n");
192 println!("CONFIGURATION:"); 261 println!("CONFIGURATION:");
193 println!(" Config location: ~/.config/oxwm/config.rs"); 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 }