Skip to content

Commit

Permalink
feat(nix): updated configuration, nixos modules and shell
Browse files Browse the repository at this point in the history
I added new option to generate filebot configuration via NixOS options
  • Loading branch information
Wittano committed Apr 15, 2024
1 parent dac683d commit bae6294
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 74 deletions.
4 changes: 2 additions & 2 deletions nix/default.nix → default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

buildGoModule rec {
name = "filebot";
src = ./../.;
src = ./.;
version = "v1.0.0";

vendorHash = "sha256-zga1pCBqisDLzDN6rO68iCQlGXmTfkUk+fqNI54yhNo=";
vendorHash = "sha256-VMLkbdTHHXYjJTFwnTK71M15lcRlhP7i+oSIiwaBPmI=";

patches = [
"./patches/fix(config)__changed_path_for_nix_build.patch"
Expand Down
21 changes: 3 additions & 18 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,12 @@
outputs = { self, nixpkgs, }:
let
pkgs = import nixpkgs { system = "x86_64-linux"; };
filebot = pkgs.callPackage ./nix/default.nix { };
filebot = pkgs.callPackage ./default.nix { };
goSDK = pkgs.go;
in
{
packages.x86_64-linux.default = filebot;
nixosModules.default = import ./nix/service.nix;
devShells.x86_64-linux.default = pkgs.mkShell {
hardeningDisable = [ "all" ];
buildInputs = with pkgs; [
# Go tools
goSDK
golangci-lint

# Nix
nixpkgs-fmt

# Github actions
act
];

GOROOT = "${goSDK}/share/go";
};
nixosModules.default = import ./service.nix;
devShells.x86_64-linux.default = import ./shell.nix { inherit pkgs goSDK; };
};
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/wittano/filebot

go 1.21
go 1.22

require (
github.com/fsnotify/fsnotify v1.7.0
Expand Down
53 changes: 0 additions & 53 deletions nix/service.nix

This file was deleted.

125 changes: 125 additions & 0 deletions service.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.services.filebot;
filebot = pkgs.callPackage ./default.nix { };

buildConfig = conf:
let
destAttrs = assert conf ? dest && (conf ? moveToTrash && conf.moveToTrash == true);
if conf ? dest
then { dest = conf.dest; }
else { moveToTrash = conf.moveToTrash; };
afterAttrs = assert conf ? after && conf.moveToTrash != true; { after = conf.after; };
in
{
name = conf.name;
value = {
src = conf.src;
exceptions = mkIf (conf ? exceptions) conf.exceptions;
recursive = mkIf (conf ? recursive) conf.recursive;
uid = conf.uid;
gid = conf.gid;
isRoot = assert cfg.user == "root"; conf.isRoot;
} // destAttrs // afterAttrs;
};

configs = builtins.listToAttrs (builtins.map buildConfig cfg.settings);

filebotConfig = (pkgs.formats.toml { }).generate "config.toml" configs;
in
{
options = {
services.filebot = {
enable = mkEnableOption "Enable filebot service";
user = mkOption {
type = types.str;
example = "wittano";
description = "Specific user, who will be run service";
};
configPath = mkOption {
type = types.path;
default = "$HOME/.setting/filebot/setting.toml";
example = "/home/wittano/setting.toml";
description = ''
Path to program configuration.
REMEMBER!
Program accept only toml-formatted files
'';
};
settings = mkOption {
type = with types; listOf (submodule {
options = {
name = mkOption {
type = str;
example = "File";
description = "Single entity of configuration. It's only nesessary for generatation config. It isn't affect on filebot";
};
isRoot = mkEnableOption "Set ownership files to root(required root permission)";
recursive = mkEnableOption "Observe files in directories inside src path";
moveToTrash = mkEnableOption "Move files to Trash directory. This option required abset 'dest' option";
after = mkOption {
type = ints.positive;
description = "Number of days, after files should move to Trash directory";
};
exceptions = mkOption {
type = listOf str;
example = [ "files.pdf" "files.ini" ];
description = "List of filenames, that should be ignored by filebot";
};
uid = mkOption {
type = ints.positive;
description = "New owner UID";
};
gid = mkOption {
type = ints.positive;
description = "New owner GID";
};
src = mkOption {
type = listOf str;
example = [ "/home/user/Documents" ];
description = "List of sources files, which should be observer";
};
dest = mkOption {
type = nullOr str;
example = "/home/user/Destination";
description = "Path to destination directory. This option can't set with moveToTrash";
};
};
});
description = "Configurtion for filebot";
};
updateInterval = mkOption {
type = types.str;
default = "10m";
example = "1h";
description = ''
Specific time, that program updates configuration ale observed files.
Program accept time in go standard format(see https://pkg.go.dev/time#ParseDuration. This page include acceptable time format).
'';
};
};
};

assertions = [
{
assertion = cfg.settings != null && cfg.configPath != null;
message = "Option services.filebot.settings and services.filebot.configPath can't be set at the same time";
}
{
assertion = cfg.settings == null && cfg.configPath == null;
message = "One of option: services.filebot.settings and services.filebot.configPath must be set";
}
];

config = mkIf cfg.enable {
systemd.services.filebot = {
description = "Service to automaticlly sorting files";
serviceConfig.User = mkIf (cfg.user != "root") cfg.user;
wantedBy = [ "multi-user.target" ];
path = [ filebot ];
script = "filebot -c ${filebotConfig} -u ${cfg.updateInterval}";
};
};
}
13 changes: 13 additions & 0 deletions shell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{ pkgs ? import <nixpkgs> { }, goSDK }: pkgs.mkShell {
hardeningDisable = [ "all" ];
nativeBuildInputs = with pkgs; [
# Go tools
goSDK

# Nix
nixpkgs-fmt
nixd
];

GOROOT = "${goSDK}/share/go";
}

0 comments on commit bae6294

Please sign in to comment.