Skip to content

Commit

Permalink
Add NixOS Flake (TamtamHero#26)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* Update flake.nix

Co-authored-by: Thomas Eizinger <[email protected]>

* Update nix/module.nix

Co-authored-by: Thomas Eizinger <[email protected]>

* 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 <[email protected]>
  • Loading branch information
Svenum and thomaseizinger committed Aug 24, 2024
1 parent fe98b44 commit e59a92d
Show file tree
Hide file tree
Showing 6 changed files with 320 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/nix-build.yml
Original file line number Diff line number Diff line change
@@ -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

44 changes: 44 additions & 0 deletions flake.lock

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

29 changes: 29 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -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;
};
}
118 changes: 118 additions & 0 deletions nix/module.nix
Original file line number Diff line number Diff line change
@@ -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
)
);
};
}
43 changes: 43 additions & 0 deletions nix/packages/fw-ectool.nix
Original file line number Diff line number Diff line change
@@ -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" ];
};
}
72 changes: 72 additions & 0 deletions nix/packages/fw-fanctrl.nix
Original file line number Diff line number Diff line change
@@ -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" ];
};
}

0 comments on commit e59a92d

Please sign in to comment.