Skip to content

Commit

Permalink
package nix & add module & refactor flake
Browse files Browse the repository at this point in the history
  • Loading branch information
id3v1669 committed Sep 18, 2024
1 parent f2bee30 commit ea68984
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 38 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
result
target
*.zip
*.gz
Expand Down
18 changes: 17 additions & 1 deletion flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 29 additions & 37 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -1,44 +1,36 @@
{
description = "Swhkd devel";
description = "Swhkd";

inputs = { nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable"; };
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
systems.url = "github:nix-systems/default-linux";
};

outputs = { self, nixpkgs, ... }:
let
pkgsFor = system:
import nixpkgs {
inherit system;
overlays = [ ];
};
outputs = inputs @
{ self
, nixpkgs
, systems
, ...
}:
let
eachSystem = nixpkgs.lib.genAttrs (import systems);

targetSystems = [ "aarch64-linux" "x86_64-linux" ];
in {
devShells = nixpkgs.lib.genAttrs targetSystems (system:
let pkgs = pkgsFor system;
in {
default = pkgs.mkShell {
name = "Swhkd-devel";
nativeBuildInputs = with pkgs; [
# Compilers
cargo
rustc
scdoc
pkgsFor = (system: import nixpkgs {
inherit system;
overlays = [ ];
});
in
{
packages = eachSystem (system: {
default = nixpkgs.legacyPackages.${system}.callPackage ./nix/package.nix{ };
});

# libs
udev
defaultPackage = eachSystem (system: self.packages.${system}.default);

# Tools
pkg-config
clippy
gdb
gnumake
rust-analyzer
rustfmt
strace
valgrind
zip
];
};
});
};
devShells = eachSystem (system: {
default = (pkgsFor system).callPackage ./nix/shell.nix { };
});

nixosModules.default = import ./nix/module.nix self;
};
}
69 changes: 69 additions & 0 deletions nix/module.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
self: {
config,
lib,
pkgs,
...
}:
let
cfg = config.services.swhkd;
inherit (pkgs.stdenv.hostPlatform) system;

inherit (lib) types;
inherit (lib.modules) mkIf;
inherit (lib.options) mkOption mkEnableOption;

format = pkgs.formats.ini {};
in {
options.services.swhkd = {
enable = mkEnableOption "Simple Wayland HotKey Daemon";

package = mkOption {
description = "The package to use for `swhkd`";
default = self.packages.${system}.default;
type = types.package;
};

cooldown = mkOption {
description = "The cooldown to use for `swhkd`";
default = 250;
type = types.int;
};

settings = mkOption {
description = "The config to use for `swhkd` syntax and samples could found in [repo](https://github.com/id3v1669/swhkd).";
default = ''
super + return
alacritty
'';
type = types.lines;
};
};

config = mkIf cfg.enable {
environment.systemPackages = [ cfg.package ];
environment.etc."swhkd/swhkdrc".text = cfg.settings;

systemd.user.services.swhkd = {
description = "Simple Wayland HotKey Daemon";
bindsTo = [ "default.target" ];
script = ''
/run/wrappers/bin/pkexec ${cfg.package}/bin/swhkd \
--cooldown ${toString cfg.cooldown}
'';
serviceConfig.Restart = "always";
wantedBy = [ "default.target" ];
};
security.polkit = {
enable = true;
extraConfig = ''
polkit.addRule(function(action, subject) {
if (action.id == "com.github.swhkd.pkexec" &&
subject.local == true &&
subject.active == true &&) {
return polkit.Result.YES;
}
});
'';
};
};
}
51 changes: 51 additions & 0 deletions nix/package.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{ lib
, rustPlatform
, makeWrapper
, pkg-config
, udev
, killall
}:

rustPlatform.buildRustPackage rec {

pname = "swhkd";
version = "1.3.0";

src = lib.cleanSource ../.;

cargoLock = {
lockFile = "${src}/Cargo.lock";
outputHashes = {
"sweet-0.3.0" = "sha256-swSE1CE39cGojp8HAziw0Bzjr+s4XaVU+4OqQDO60fE=";
};
};

nativeBuildInputs = [
pkg-config
makeWrapper
];

buildInputs = [
udev
killall
];

postInstall = ''
mkdir -p $out/share/polkit-1/actions
cat > $out/share/polkit-1/actions/com.github.swhkd.pkexec.policy <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE policyconfig PUBLIC "-//freedesktop//DTD PolicyKit Policy Configuration 1.0//EN" "http://www.freedesktop.org/standards/PolicyKit/1/policyconfig.dtd">
<policyconfig>
<action id="com.github.swhkd.pkexec">
<message>Authentication is required to run Simple Wayland Hotkey Daemon</message>
<defaults>
<allow_any>no</allow_any>
<allow_inactive>no</allow_inactive>
<allow_active>yes</allow_active>
</defaults>
<annotate key="org.freedesktop.policykit.exec.path">$out/bin/swhkd</annotate>
</action>
</policyconfig>
EOF
'';
}
26 changes: 26 additions & 0 deletions nix/shell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{pkgs}:
pkgs.mkShell {
name = "Swhkd-devel";
nativeBuildInputs = with pkgs; [
# Compilers
cargo
rustc
scdoc

# libs
udev

# Tools
cargo-audit
cargo-deny
pkg-config
clippy
gdb
gnumake
rust-analyzer
rustfmt
strace
valgrind
zip
];
}

0 comments on commit ea68984

Please sign in to comment.