Skip to content

Commit

Permalink
Merge pull request #2 from Lenivaya/feat/nix
Browse files Browse the repository at this point in the history
  • Loading branch information
Lenivaya authored Oct 27, 2024
2 parents 0864009 + b969355 commit 56029da
Show file tree
Hide file tree
Showing 9 changed files with 207 additions and 118 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
name: Tests

on:
pull_request:
paths:
Expand All @@ -9,7 +8,6 @@ on:
- ".github/.codecov.yml"
branches:
- "*"

jobs:
test_unix:
runs-on: ubuntu-latest
Expand All @@ -19,7 +17,6 @@ jobs:
uses: DeterminateSystems/nix-installer-action@v10
- name: Run the Magic Nix Cache
uses: DeterminateSystems/magic-nix-cache-action@v4

- name: Cache Rust dependencies
uses: actions/cache@v3
with:
Expand All @@ -30,6 +27,5 @@ jobs:
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
restore-keys: |
${{ runner.os }}-cargo-
- name: Test
run: nix develop .#ci-tests --command cargo test --verbose --all
11 changes: 0 additions & 11 deletions .github/workflows/trunk-format.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
name: Format and Lint

on:
push:
branches:
Expand All @@ -14,24 +13,18 @@ on:
- "**.rs"
- "Cargo*"
- ".github/workflows/**.yml"

jobs:
format-and-lint:
runs-on: ubuntu-latest

permissions:
contents: write
id-token: write

steps:
- uses: actions/checkout@v4

- name: Install Nix
uses: DeterminateSystems/nix-installer-action@v10

- name: Run the Magic Nix Cache
uses: DeterminateSystems/magic-nix-cache-action@v4

- name: Cache Rust dependencies
uses: actions/cache@v3
with:
Expand All @@ -42,17 +35,13 @@ jobs:
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
restore-keys: |
${{ runner.os }}-cargo-
- uses: rrbutani/use-nix-shell-action@v1
with:
devShell: .#ci-format

- name: Format project source code
run: nix fmt

- name: Run Clippy
run: nix develop .#ci-format --command cargo clippy --all-features -- -D warnings

- name: Commit formatting changes
uses: stefanzweifel/git-auto-commit-action@v5
with:
Expand Down
143 changes: 41 additions & 102 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -13,108 +13,47 @@
};

outputs =
inputs@{
nixpkgs,
flake-parts,
naersk,
treefmt-nix,
gitignore,
...
}:
flake-parts.lib.mkFlake { inherit inputs; } {
imports = [
treefmt-nix.flakeModule
flake-parts.flakeModules.easyOverlay
];

systems = nixpkgs.lib.systems.flakeExposed;
perSystem =
{
pkgs,
self',
lib,
...
}:
let
naersk' = pkgs.callPackage naersk { };
inherit (gitignore.lib) gitignoreSource;
src = gitignoreSource ./.;
in
{
overlayAttrs = {
inherit (self'.packages) resterrs;
};

packages.resterrs = naersk'.buildPackage {
inherit src;
buildInputs = with pkgs; [
pkg-config
systemd
];
};
packages.default = self'.packages.resterrs;

devShells =
let
bareMinimum = with pkgs; [
rustc
cargo

systemd
pkg-config
];
in
{
default = pkgs.mkShell {
name = "resterrs-dev";
nativeBuildInputs =
with pkgs;
bareMinimum
++ [
cargo-tarpaulin
cargo-edit

rustfmt
clippy

act
];
RUST_BACKTRACE = 1;
RUST_LOG = "warn,test,info";
};

ci-tests = pkgs.mkShell {
name = "resterrs-ci";
nativeBuildInputs = bareMinimum ++ (with pkgs; [ cargo-tarpaulin ]);
RUST_BACKTRACE = 1;
};

ci-format = pkgs.mkShell {
name = "resterrs-ci-format";
nativeBuildInputs =
bareMinimum
++ (with pkgs; [
rustfmt
clippy
]);
};

testing = pkgs.mkShell {
name = "resterrs-test";
nativeBuildInputs = [ self'.packages.default ];
};
};

treefmt = {
projectRootFile = "flake.nix";

programs = {
nixfmt.enable = true;
rustfmt.enable = true;
yamlfmt.enable = true;
prettier.enable = true;
inputs@{ flake-parts, ... }:
flake-parts.lib.mkFlake { inherit inputs; } (
{
withSystem,
moduleWithSystem,
flake-parts-lib,
...
}:
let
inherit (flake-parts-lib) importApply;
resterrs-flake-module = importApply ./nix/resterrs {
inherit withSystem moduleWithSystem importApply;
};
in
{
imports = [
inputs.treefmt-nix.flakeModule
inputs.flake-parts.flakeModules.easyOverlay
resterrs-flake-module
];

systems = [
"x86_64-linux"
"aarch64-linux"
];

perSystem =
{
config,
self',
pkgs,
lib,
...
}:
{
overlayAttrs = {
inherit (self'.packages) resterrs;
};
devShells = import ./nix/shell.nix { inherit pkgs self'; };
treefmt = import ./nix/treefmt.nix { };
};
};
};
}
);
}
34 changes: 34 additions & 0 deletions nix/resterrs/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# The importApply argument. Use this to reference things defined locally,
# as opposed to the flake where this is imported.
localFlake:

# Regular module arguments; self, inputs, etc all reference the final user flake,
# where this module was imported.
{
lib,
config,
self,
inputs,
...
}:
let
resterrsMod = localFlake.moduleWithSystem (
perSystem@{ config }: localFlake.importApply ./nixosModules perSystem
);
in
{
perSystem =
{ system, ... }:
let
resterrs' = localFlake.withSystem system (
{ config, pkgs, ... }: pkgs.callPackage ./pkgs/resterrs.nix { inherit inputs; }
);
in
{
packages.resterrs = resterrs';
packages.default = resterrs';
};

flake.nixosModules.resterrs = resterrsMod;
flake.nixosModules.default = resterrsMod;
}
54 changes: 54 additions & 0 deletions nix/resterrs/nixosModules/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
perSystem:
{
config,
pkgs,
lib,
...
}:
with lib;
let
cfg = config.services.resterrs;

inherit (lib.meta) getExe';
inherit (lib.options) mkEnableOption mkPackageOption mkOption;

toTOML = pkgs.formats.toml { };
in
{
options.services.resterrs = {
enable = mkEnableOption "Resterrs, simple rester";
package = mkPackageOption pkgs "resterrs" { } // {
default = perSystem.config.packages.resterrs;
};

settings = mkOption {
type = toTOML.type;
default = { };
example = literalExpression ''
system_services_to_stop = ["bpftune", "syncthing", "fwupd"]
user_services_to_stop = ["kdeconnect", "picom"]
apps_to_stop = ["telegram-desktop", "vesktop"]
username = "leniviy"
'';
description = "TOML configuration for Resterrs. See the Resterrs documentation for available options.";
};
};

config = mkIf cfg.enable {
systemd.services.resterrs = {
description = "Resterrs Service";
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
serviceConfig = {
ExecStart = "${getExe' cfg.package "resterrs"} -c ${toTOML.generate "resterrs-config.toml" cfg.settings}";
Restart = "always";
RestartSec = "10";
User = "root";
DynamicUser = true;
StateDirectory = "resterrs";
ConfigurationDirectory = "resterrs";
RuntimeDirectory = "resterrs";
};
};
};
}
17 changes: 17 additions & 0 deletions nix/resterrs/pkgs/resterrs.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
inputs,
pkgs,
...
}:
let
naersk' = pkgs.callPackage inputs.naersk { };
inherit (inputs.gitignore.lib) gitignoreSource;
src = gitignoreSource ../../../.;
in
naersk'.buildPackage {
inherit src;
buildInputs = with pkgs; [
pkg-config
systemd
];
}
50 changes: 50 additions & 0 deletions nix/shell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{ pkgs, self' }:
let
bareMinimum = with pkgs; [
rustc
cargo

systemd
pkg-config
];
in
{
default = pkgs.mkShell {
name = "resterrs-dev";
nativeBuildInputs =
with pkgs;
bareMinimum
++ [
cargo-tarpaulin
cargo-edit

rustfmt
clippy

act
];
RUST_BACKTRACE = 1;
RUST_LOG = "warn,test,info";
};

ci-tests = pkgs.mkShell {
name = "resterrs-ci";
nativeBuildInputs = bareMinimum ++ (with pkgs; [ cargo-tarpaulin ]);
RUST_BACKTRACE = 1;
};

ci-format = pkgs.mkShell {
name = "resterrs-ci-format";
nativeBuildInputs =
bareMinimum
++ (with pkgs; [
rustfmt
clippy
]);
};

testing = pkgs.mkShell {
name = "resterrs-test";
nativeBuildInputs = [ self'.packages.default ];
};
}
10 changes: 10 additions & 0 deletions nix/treefmt.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
_: {
projectRootFile = "flake.nix";

programs = {
nixfmt.enable = true;
rustfmt.enable = true;
yamlfmt.enable = true;
prettier.enable = true;
};
}
Loading

0 comments on commit 56029da

Please sign in to comment.