diff --git a/nix/default.nix b/default.nix similarity index 66% rename from nix/default.nix rename to default.nix index db4dca7..a216791 100644 --- a/nix/default.nix +++ b/default.nix @@ -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" diff --git a/flake.nix b/flake.nix index 8888972..7f18796 100644 --- a/flake.nix +++ b/flake.nix @@ -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; }; }; } diff --git a/go.mod b/go.mod index de38fc5..95f1d80 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/wittano/filebot -go 1.21 +go 1.22 require ( github.com/fsnotify/fsnotify v1.7.0 diff --git a/nix/service.nix b/nix/service.nix deleted file mode 100644 index 5fba774..0000000 --- a/nix/service.nix +++ /dev/null @@ -1,53 +0,0 @@ -{ config, lib, pkgs, ... }: - -let - cfg = config.services.filebot; - program = pkgs.callPackage ./default.nix { }; -in -{ - options = { - services.filebot = { - enable = lib.mkEnableOption "Enable filebot service"; - user = lib.mkOption { - type = lib.types.str; - example = "wittano"; - description = '' - Specific user, who will be run service - ''; - }; - configPath = lib.mkOption { - type = lib.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 - ''; - }; - updateInterval = lib.mkOption { - type = lib.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). - ''; - }; - }; - }; - - config = lib.mkIf cfg.enable { - systemd.services.filebot = { - description = '' - Service to automaticlly sorting files - ''; - serviceConfig.User = "${cfg.user}"; - wantedBy = [ "multi-user.target" ]; - script = '' - ${program}/bin/filebot -c ${builtins.toString cfg.configPath} -u ${cfg.updateInterval} - ''; - }; - }; -} diff --git a/service.nix b/service.nix new file mode 100644 index 0000000..b1d77aa --- /dev/null +++ b/service.nix @@ -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}"; + }; + }; +} diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..dc45f2a --- /dev/null +++ b/shell.nix @@ -0,0 +1,13 @@ +{ pkgs ? import { }, goSDK }: pkgs.mkShell { + hardeningDisable = [ "all" ]; + nativeBuildInputs = with pkgs; [ + # Go tools + goSDK + + # Nix + nixpkgs-fmt + nixd + ]; + + GOROOT = "${goSDK}/share/go"; +}