Skip to content

Commit

Permalink
fix(nix): format nix files and fix home-manager module
Browse files Browse the repository at this point in the history
  • Loading branch information
akshettrj committed Nov 22, 2024
1 parent cf79538 commit 2af3d72
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 144 deletions.
27 changes: 19 additions & 8 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,29 @@
flake-utils.url = "github:numtide/flake-utils";
nix-filter.url = "github:numtide/nix-filter";
gomod2nix = {
url = "github:nix-community/gomod2nix";
inputs.nixpkgs.follows = "nixpkgs";
url = "github:nix-community/gomod2nix";
inputs.nixpkgs.follows = "nixpkgs";
};
};

outputs = { self, nixpkgs, flake-utils, gomod2nix, nix-filter }:
flake-utils.lib.eachDefaultSystem (system:
outputs =
{
self,
nixpkgs,
flake-utils,
gomod2nix,
nix-filter,
}:
flake-utils.lib.eachDefaultSystem (
system:
let
pkgs = import nixpkgs {
inherit system;
overlays = [ gomod2nix.overlays.default ];
inherit system;
overlays = [ gomod2nix.overlays.default ];
};
in
with pkgs; {
with pkgs;
{

devShells.default = mkShell {
name = "watgbridge-dev";
Expand Down Expand Up @@ -51,6 +60,8 @@

nixConfig = {
extra-substituters = [ "https://watgbridge.cachix.org" ];
extra-trusted-public-keys = [ "watgbridge.cachix.org-1:KSfgmbSBvXQTpUnoCj21vST7zgwpy3SbNfk0/nesR1Y=" ];
extra-trusted-public-keys = [
"watgbridge.cachix.org-1:KSfgmbSBvXQTpUnoCj21vST7zgwpy3SbNfk0/nesR1Y="
];
};
}
139 changes: 72 additions & 67 deletions nix/modules/commonOptions.nix
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
{
lib,
package,
}: let
forNixos,
}:
let
inherit (lib) mkEnableOption mkOption types;
in {
in
{
enable = mkEnableOption "WaTgBridge services";

commonSettings.package = mkOption {
Expand All @@ -22,71 +25,73 @@ in {
};

instances = mkOption {
type = types.attrsOf(types.submodule {
options = {

enable = mkEnableOption "Enable the instance";

name = mkOption {
type = types.nullOr types.str;
default = null;
description = ''
The name of the instance. The corresponding systemd service will be called `watgbridge-<name>.service`.
By default, the key of the instance in the attrset will be used.
'';
};

package = mkOption {
type = types.nullOr types.package;
default = null;
description = ''
The WaTgBridge package to use for the instance.
By default the common package defined will be used.
'';
};

configPath = mkOption {
type = types.nullOr (types.either (types.str types.path));
default = null;
description = ''
The path to the config file that will be loaded by WaTgBridge.
By default, WaTgBridge loads config from config.yaml in the current working directory.
'';
};

workingDirectory = mkOption {
type = types.nullOr types.str;
default = null;
description = ''
The directory from which the WaTgBridge binary will be executed.
If not provided, it will use systemd's default working directory.
'';
};

maxRuntime = mkOption {
type = types.nullOr types.str;
default = null;
description = ''
Max time in seconds (according to systemd's units) after which the service will be automatically restarted.
If not provided, it will use the common setting (which is 1d, i.e. 1 day).
'';
};

requires = mkOption {
type = types.nullOf (types.listOf types.str);
default = null;
description = ''
The systemd services to wait for before starting watbridge. "network.target" is added to the module itself. This option is meant to be used for stuff like Telegram Bot API service.
If not provided, it will use the common setting
'';
type = types.attrsOf (
types.submodule {
options = {

enable = mkEnableOption "Enable the instance";

name = mkOption {
type = types.nullOr types.str;
default = null;
description = ''
The name of the instance. The corresponding systemd service will be called `watgbridge-<name>.service`.
By default, the key of the instance in the attrset will be used.
'';
};

package = mkOption {
type = types.nullOr types.package;
default = null;
description = ''
The WaTgBridge package to use for the instance.
By default the common package defined will be used.
'';
};

configPath = mkOption {
type = types.nullOr (types.either (types.str types.path));
default = null;
description = ''
The path to the config file that will be loaded by WaTgBridge.
By default, WaTgBridge loads config from config.yaml in the current working directory.
'';
};

workingDirectory = mkOption {
type = types.nullOr types.str;
default = null;
description = ''
The directory from which the WaTgBridge binary will be executed.
If not provided, it will use systemd's default working directory.
'';
};

maxRuntime = mkOption {
type = types.nullOr types.str;
default = null;
description = ''
Max time in seconds (according to systemd's units) after which the service will be automatically restarted.
If not provided, it will use the common setting (which is 1d, i.e. 1 day).
'';
};

requires = mkOption {
type = types.nullOf (types.listOf types.str);
default = null;
description = ''
The systemd services to wait for before starting watbridge. "network.target" is added to the module itself. This option is meant to be used for stuff like Telegram Bot API service.
If not provided, it will use the common setting
'';
};
};
};
});
}
);
};
}
98 changes: 51 additions & 47 deletions nix/modules/home-manager/default.nix
Original file line number Diff line number Diff line change
@@ -1,74 +1,78 @@
self: {
self:
{
lib,
config,
pkgs,
...
}: let
}:
let
inherit (pkgs.stdenv.hostPlatform) system;
inherit (lib) mapAttrs' mkIf;
cfg = config.services.watgbridge;

package = self.packages."${system}".watgbridge;
in {
in
{
options = {
services.watgbridge = import ../commonOptions.nix { inherit lib package; };
};

config = mkIf cfg.enable {
home.packages = [ cfg.package ];

systemd.user.services = mapAttrs' (key: settings: let
systemd.user.services = mapAttrs' (
key: settings:
let

instanceName = (
if settings.name != null then
"watgbridge-${settings.name}"
else
"watgbridge-${key}"
);
watgbridgePackage = (
if settings.package != null then
settings.package
else
cfg.commonSettings.package
);
instanceName = (
if settings.name != null then "watgbridge-${settings.name}" else "watgbridge-${key}"
);
watgbridgePackage = (
if settings.package != null then settings.package else cfg.commonSettings.package
);

maxRuntime = (
if settings.maxRuntime != null then
settings.maxRuntime
else
cfg.commonSettings.maxRuntime
);
maxRuntime = (
if settings.maxRuntime != null then settings.maxRuntime else cfg.commonSettings.maxRuntime
);

requires = (
if settings.requires != null then
settings.requires
else
cfg.commonSettings.requires
);
requires = (if settings.requires != null then settings.requires else cfg.commonSettings.requires);

in {
in
{

name = instanceName;
name = instanceName;

value = mkIf settings.enable {
Unit = {
Description = "WaTgBridge service for '${instanceName}'";
Documentation = "https://github.com/akshettrj/watbridge";
After = [ "network.target" ];
} // lib.optionalAttrs (requires != null) {
Requires = requires;
};
value = mkIf settings.enable {
Unit =
{
Description = "WaTgBridge service for '${instanceName}'";
Documentation = "https://github.com/akshettrj/watbridge";
After = [ "network.target" ];
}
// lib.optionalAttrs (requires != null) {
Requires = requires;
};

Service = {
ExecStart = ''${watgbridgePackage}/bin/watbridge'' lib.optionalString (settings.configPath != null) '' "${settings.configPath}"'';
Restart = "on-failure";
} // lib.optionalAttrs maxRuntime != null {
RuntimeMaxSec = maxRuntime;
};
Service =
{
ExecStart =
''${watgbridgePackage}/bin/watbridge''
+ lib.optionalString (settings.configPath != null) ''"${settings.configPath}"'';
Restart = "on-failure";
}
// lib.optionalAttrs maxRuntime != null {
RuntimeMaxSec = maxRuntime;
}
// lib.optionalAttrs (settings.workingDirectory != null) {
WorkingDirectory = settings.workingDirectory;
};

Install = { WantedBy = ["default.target"]; };
};
Install = {
WantedBy = [ "default.target" ];
};
};

}) cfg.instances;
}
) cfg.instances;
};
}
19 changes: 12 additions & 7 deletions nix/modules/nixos/default.nix
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
self: {
self:
{
lib,
config,
pkgs,
...
}: let
}:
let
inherit (pkgs.stdenv.hostPlatform) system;
inherit (lib) mkIf;
cfg = config.services.watgbridge;

package = self.packages."${system}".watgbridge;
in {
in
{
options = {
services.watgbridge = import ../commonOptions.nix { inherit lib package; };
};

config = mkIf cfg.enable {
assertions = [{
assertion = false;
message = "The NixOS module is not complete yet. Use home-manager module for now if possible.";
}];
assertions = [
{
assertion = false;
message = "The NixOS module is not complete yet. Use home-manager module for now if possible.";
}
];

environment.systemPackages = [ cfg.package ];
};
Expand Down
Loading

0 comments on commit 2af3d72

Please sign in to comment.