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
2 -, rustPlatform
3 -, pkg-config
4 -, xorg
5 -, freetype
6 -, fontconfig
1 +{
2 + lib,
3 + rustPlatform,
4 + pkg-config,
5 + xorg,
6 + freetype,
7 + fontconfig,
8 + makeDesktopItem,
7 9 }:
8 -
9 -rustPlatform.buildRustPackage {
10 +rustPlatform.buildRustPackage (finalAttrs: {
10 11 pname = "oxwm";
11 12 version = "0.1.0";
12 13
@@ -28,12 +29,24 @@ rustPlatform.buildRustPackage {
28 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 45 meta = with lib; {
32 46 description = "A dynamic window manager written in Rust, inspired by dwm";
33 47 homepage = "https://github.com/tonybanters/oxwm";
34 48 license = licenses.gpl3;
35 - maintainers = [ ];
36 49 platforms = platforms.linux;
37 50 mainProgram = "oxwm";
38 51 };
39 -}
52 +})
+26 −47 flake.nix
@@ -7,19 +7,16 @@
7 7 self,
8 8 nixpkgs,
9 9 }: let
10 - systems = ["x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin"];
11 - forAllSystems = nixpkgs.lib.genAttrs systems;
10 + systems = ["x86_64-linux" "aarch64-linux"];
11 +
12 + forAllSystems = fn: nixpkgs.lib.genAttrs systems (system: fn nixpkgs.legacyPackages.${system});
12 13 in {
13 - packages = forAllSystems (system: let
14 - pkgs = import nixpkgs {inherit system;};
15 - in {
14 + packages = forAllSystems (pkgs: rec {
16 15 default = pkgs.callPackage ./default.nix {};
17 - oxwm = self.packages.${system}.default;
16 + oxwm = default;
18 17 });
19 18
20 - devShells = forAllSystems (system: let
21 - pkgs = import nixpkgs {inherit system;};
22 - in {
19 + devShells = forAllSystems (pkgs: {
23 20 default = pkgs.mkShell {
24 21 buildInputs = [
25 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 42 nixosModules.default = {
46 43 config,
47 44 lib,
48 45 pkgs,
49 46 ...
50 - }:
51 - with lib; let
52 - cfg = config.services.xserver.windowManager.oxwm;
53 - oxwmDesktopItem = pkgs.writeTextFile {
54 - name = "oxwm.desktop";
55 - destination = "/share/xsessions/oxwm.desktop";
56 - text = ''
57 - [Desktop Entry]
58 - Name=OXWM
59 - Comment=A dynamic window manager written in Rust
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];
47 + }: let
48 + inherit (lib) mkEnableOption mkOption mkIf types;
49 + cfg = config.services.xserver.windowManager.oxwm;
50 + in {
51 + options.services.xserver.windowManager.oxwm = {
52 + enable = mkEnableOption "oxwm window manager";
53 + package = mkOption {
54 + type = types.package;
55 + default = self.packages.${pkgs.system}.default;
56 + description = "The oxwm package to use";
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 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 }