From e59a92de31e12f081bbc7c2cd026037158567849 Mon Sep 17 00:00:00 2001 From: Svenum <43136984+Svenum@users.noreply.github.com> Date: Sat, 1 Jun 2024 19:32:35 +0200 Subject: [PATCH] Add NixOS Flake (#26) * initial * update gitignore * update inputs * add fw-ectool dependencie * add module * fix tabs * fix package * fix typo * fix service * fix type * add options * fix service * fix build inputs * add Readme + add suspend script * remove unneeded }; * fix pkgs.writeShellScript * remvoe \ * try * add self * fix module * update package * fix package * use sleep script * add config options * fix typo * fix typo * add defaults * fix type * add prettyier * remove beautifyer * udpate readme * update installer script * add missing path * Update README.md Co-authored-by: Thomas Eizinger * Update flake.nix Co-authored-by: Thomas Eizinger * Update nix/module.nix Co-authored-by: Thomas Eizinger * add descriptions * fix uninstall * update readme * add description * remove requiremetns.txt + add github actions * update action * rename workflow test * fix service * try * try * Update README.md * Update README.md * chagne flake description * fix suspend script * fix script * fix path * fix install.sh * fix --no-sudo * add --no-sudo to other scripts * fix check * add option check * add missing " * Rename nix action --------- Co-authored-by: Thomas Eizinger --- .github/workflows/nix-build.yml | 14 ++++ flake.lock | 44 ++++++++++++ flake.nix | 29 ++++++++ nix/module.nix | 118 ++++++++++++++++++++++++++++++++ nix/packages/fw-ectool.nix | 43 ++++++++++++ nix/packages/fw-fanctrl.nix | 72 +++++++++++++++++++ 6 files changed, 320 insertions(+) create mode 100644 .github/workflows/nix-build.yml create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 nix/module.nix create mode 100644 nix/packages/fw-ectool.nix create mode 100644 nix/packages/fw-fanctrl.nix diff --git a/.github/workflows/nix-build.yml b/.github/workflows/nix-build.yml new file mode 100644 index 0000000..27a5605 --- /dev/null +++ b/.github/workflows/nix-build.yml @@ -0,0 +1,14 @@ +name: "Nix packaging test" +on: + pull_request: + +jobs: + Build-Package: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: cachix/install-nix-action@v27 + with: + nix_path: nixpkgs=channel:nixos-unstable + - run: nix build + diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..8de5f21 --- /dev/null +++ b/flake.lock @@ -0,0 +1,44 @@ +{ + "nodes": { + "flake-compat": { + "flake": false, + "locked": { + "lastModified": 1696426674, + "narHash": "sha256-kvjfFW7WAETZlt09AgDn1MrtKzP7t90Vf7vypd3OL1U=", + "owner": "edolstra", + "repo": "flake-compat", + "rev": "0f9255e01c2351cc7d116c072cb317785dd33b33", + "type": "github" + }, + "original": { + "owner": "edolstra", + "repo": "flake-compat", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1713564160, + "narHash": "sha256-YguPZpiejgzLEcO36/SZULjJQ55iWcjAmf3lYiyV1Fo=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "bc194f70731cc5d2b046a6c1b3b15f170f05999c", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-23.11", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-compat": "flake-compat", + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..1c8816a --- /dev/null +++ b/flake.nix @@ -0,0 +1,29 @@ +{ + description = "A simple systemd service to better control Framework Laptop's fan(s)"; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.11"; + + flake-compat = { + url = "github:edolstra/flake-compat"; + flake = false; + }; + }; + + outputs = { self, nixpkgs, flake-compat }: { + packages.x86_64-linux.default = self.packages.x86_64-linux.fw-fanctrl; + packages.x86_64-linux.fw-fanctrl = ( + import nixpkgs { + currentSystem = "x86_64-linux"; + localSystem = "x86_64-linux"; + }).pkgs.callPackage ./nix/packages/fw-fanctrl.nix {}; + + packages.x86_64-linux.fw-ectool = ( + import nixpkgs { + currentSystem = "x86_64-linux"; + localSystem = "x86_64-linux"; + }).pkgs.callPackage ./nix/packages/fw-ectool.nix {}; + + nixosModules.default = import ./nix/module.nix; + }; +} diff --git a/nix/module.nix b/nix/module.nix new file mode 100644 index 0000000..7aa53c9 --- /dev/null +++ b/nix/module.nix @@ -0,0 +1,118 @@ +{ options, config, lib, pkgs, stdenv, ... }: + +with lib; +with lib.types; +let + cfg = config.programs.fw-fanctrl; + fw-ectool = pkgs.callPackage ./packages/fw-ectool.nix {}; + fw-fanctrl = pkgs.callPackage ./packages/fw-fanctrl.nix {}; + defaultConfig = builtins.fromJSON (builtins.readFile ../config.json); +in +{ + options.programs.fw-fanctrl = { + enable = mkOption { + type = bool; + default = false; + description = '' + Enable fw-fanctrl systemd service and install the needed packages. + ''; + }; + config = { + defaultStrategy = mkOption { + type = str; + default = defaultConfig.defaultStrategy; + description = "Default strategy to use"; + }; + strategyOnDischarging = mkOption { + type = str; + default = defaultConfig.strategyOnDischarging; + description = "Default strategy on discharging"; + }; + batteryChargingStatusPath = mkOption { + type = str; + default = "/sys/class/power_supply/BAT1/status"; + description = ""; + }; + strategies = mkOption { + default = defaultConfig.strategies; + type = attrsOf (submodule ( + { options, name, ... }: + { + options = { + name = mkOption { + type = str; + default = ""; + description = "Name of the strategy"; + }; + fanSpeedUpdateFrequency = mkOption { + type = int; + default = 5; + description = "How often the fan speed should be updated in seconds"; + }; + movingAverageInterval = mkOption { + type = int; + default = 25; + description = "Interval (seconds) of the last temperatures to use to calculate the average temperature"; + }; + speedCurve = mkOption { + default = []; + description = "How should the speed curve look like"; + type = listOf (submodule ( + { options, ... }: + { + options = { + temp = mkOption { + type = int; + default = 0; + description = "Temperature at which the fan speed should be changed"; + }; + speed = mkOption { + type = int; + default = 0; + description = "Percent how fast the fan should run at"; + }; + }; + } + )); + }; + }; + } + )); + }; + }; + }; + + config = mkIf cfg.enable { + # Install package + environment.systemPackages = [ + fw-fanctrl + fw-ectool + ]; + + # Create config + environment.etc."fw-fanctrl/config.json" = { + text = builtins.toJSON cfg.config; + }; + + # Create Service + systemd.services.fw-fanctrl = { + description = "Framework Fan Controller"; + after = [ "multi-user.target" ]; + serviceConfig = { + Type = "simple"; + Restart = "always"; + ExecStart = "${fw-fanctrl}/bin/fw-fanctrl --run --config /etc/fw-fanctrl/config.json --no-log"; + ExecStopPost = "${fw-ectool}/bin/ectool autofanctrl"; + }; + enable = true; + wantedBy = [ "multi-user.target" ]; + }; + + # Create suspend config + environment.etc."systemd/system-sleep/fw-fanctrl-suspend.sh".source = pkgs.writeShellScript "fw-fanctrl-suspend" ( + builtins.replaceStrings [ ''/usr/bin/python3 "%PREFIX_DIRECTORY%/bin/fw-fanctrl"'' "/bin/bash" ] [ "${fw-fanctrl}/bin/fw-fanctrl" "" ] ( + builtins.readFile ../services/system-sleep/fw-fanctrl-suspend + ) + ); + }; +} diff --git a/nix/packages/fw-ectool.nix b/nix/packages/fw-ectool.nix new file mode 100644 index 0000000..1900116 --- /dev/null +++ b/nix/packages/fw-ectool.nix @@ -0,0 +1,43 @@ +{ + stdenv, + lib, + autoPatchelfHook, + libusb1, + libftdi1 +}: + +stdenv.mkDerivation { + version = "20-04-2024"; + name = "fw-ectool"; + src = ../../.; + + outputs = [ "out" ]; + + nativeBuildInputs = [ + autoPatchelfHook + ]; + + propagatedBuildInputs = [ + libusb1 + libftdi1 + ]; + + installPhase = '' + mkdir -p $out/bin + runHook preInstall + install -m755 ./bin/ectool $out/bin/ectool + ln -s $out/bin/ectool $out/bin/fw-ectool + chmod -R 755 $out/bin/* + ''; + + doCheck = false; + + meta = with lib; { + mainProgram = "ectool"; + homepage = "https://github.com/TamtamHero/fw-fanctrl"; + description = "fw-ectool customized for fw-fanctrl"; + platforms = with platforms; linux; + license = licenses.bsd3; + maintainers = with maintainers; [ "Svenum" ]; + }; +} diff --git a/nix/packages/fw-fanctrl.nix b/nix/packages/fw-fanctrl.nix new file mode 100644 index 0000000..0e207fa --- /dev/null +++ b/nix/packages/fw-fanctrl.nix @@ -0,0 +1,72 @@ +{ +lib, +python3Packages, +python3, +bash, +callPackage, +getopt +}: + +let + pversion = "20-04-2024"; + description = "A simple systemd service to better control Framework Laptop's fan(s)"; + url = "https://github.com/TamtamHero/fw-fanctrl"; +in +python3Packages.buildPythonPackage rec{ + pname = "fw-fanctrl"; + version = pversion; + + src = ../../.; + + outputs = [ "out" ]; + + preBuild = '' + cat > setup.py << EOF + from setuptools import setup + + setup( + name="fw-fanctrl", + description="${description}", + url="${url}", + platforms=["linux"], + py_modules=[], + + scripts=[ + "fanctrl.py", + ], + ) + EOF + ''; + + nativeBuildInputs = [ + python3 + getopt + bash + ]; + + propagatedBuildInputs = [ + (callPackage ./fw-ectool.nix {}) + ]; + + doCheck = false; + + postPatch = '' + patchShebangs --build fanctrl.py + patchShebangs --build install.sh + ''; + + installPhase = '' + ./install.sh --dest-dir $out --prefix-dir "" --no-ectool --no-post-install --no-sudo + rm -rf $out/etc + rm -rf $out/lib + ''; + + meta = with lib; { + mainProgram = "fw-fanctrl"; + homepage = url; + description = description; + platforms = with platforms; linux; + license = licenses.bsd3; + maintainers = with maintainers; [ "Svenum" ]; + }; +}