diff --git a/concraft-sgjp-model.nix b/concraft-sgjp-model.nix new file mode 100644 index 0000000..d268179 --- /dev/null +++ b/concraft-sgjp-model.nix @@ -0,0 +1,5 @@ +{fetchurl, ...}: +fetchurl { + url = "https://zil.ipipan.waw.pl/Concraft?action=AttachFile&do=get&target=concraft-pl-model-SGJP-20220221.gz"; + hash = "sha256-VcvdSkJwUhAgHroA0d/bH3QDjjO/2x8HqSuUvRgIN/4="; +} diff --git a/conmorfeusz.nix b/conmorfeusz.nix index 31252e0..77c5484 100644 --- a/conmorfeusz.nix +++ b/conmorfeusz.nix @@ -1,38 +1,35 @@ { self, + pkgs, stdenv, lib, writers, + writeScriptBin, nodejs, fetchurl, - model ? "", + model ? null, serverUrl ? "", local ? true, ... -}: let - sgjpModel = fetchurl { - url = "https://zil.ipipan.waw.pl/Concraft?action=AttachFile&do=get&target=concraft-pl-model-SGJP-20220221.gz"; - hash = "sha256-VcvdSkJwUhAgHroA0d/bH3QDjjO/2x8HqSuUvRgIN/4="; - }; +}: +writeScriptBin "conmorfeusz" '' + #!/${nodejs}/bin/node + // Passed from Nix derivation config + const MORFEUSZ_ANALYSER_BIN="${self.packages.${stdenv.hostPlatform.system}.morfeusz}/bin/morfeusz_analyzer"; + const MORFEUSZ_GENERATOR_BIN="${self.packages.${stdenv.hostPlatform.system}.morfeusz}/bin/morfeusz_generator"; + const CONCRAFT_BIN="${self.packages.${stdenv.hostPlatform.system}.concraft-pl}/bin/concraft-pl"; + const CONCRAFT_MODEL="${ + if model != null + then model + else pkgs.callPackage ./concraft-sgjp-model.nix {} + }"; + const CONCRAFT_SERVER_URL="${serverUrl}"; + const CONCRAFT_MODE="${ + if local + then "local" + else "remote" + }"; + // ================================= - targetModel = - if model == "" - then sgjpModel - else model; -in - writers.makeScriptWriter {interpreter = "${nodejs}/bin/node";} "conmorfeusz" '' - // Passed from Nix derivation config - const MORFEUSZ_ANALYSER_BIN="${self.packages.${stdenv.hostPlatform.system}.morfeusz}/bin/morfeusz_analyzer"; - const MORFEUSZ_GENERATOR_BIN="${self.packages.${stdenv.hostPlatform.system}.morfeusz}/bin/morfeusz_generator"; - const CONCRAFT_BIN="${self.packages.${stdenv.hostPlatform.system}.concraft-pl}/bin/concraft-pl"; - const CONCRAFT_MODEL="${targetModel}"; - const CONCRAFT_SERVER_URL="${serverUrl}"; - const CONCRAFT_MODE="${ - if local - then "local" - else "remote" - }"; - // ================================= - - ${builtins.readFile ./index.js} - '' + ${builtins.readFile ./index.js} +'' diff --git a/index.js b/index.js index 792626d..f7107d7 100755 --- a/index.js +++ b/index.js @@ -97,7 +97,7 @@ async function danalyse(input) { return await desambiguate(analysis); } -function generate(text, ...tags) { +function generate(input, ...tags) { const { stdout } = spawnSync(MORFEUSZ_GENERATOR_BIN, [], { input }); const linePattern = /^\s?(?\[)?(?,|(.+?)),(?,|(.+?)),(?[\w:.]+),(?_|(.+?)),(?_|(.+?))(?\])?$/; diff --git a/module.nix b/module.nix index ea8f50d..18f5d75 100644 --- a/module.nix +++ b/module.nix @@ -1,2 +1,67 @@ -{...}: { +self: { + config, + system, + pkgs, + lib, + ... +}: let + cfg = config.services.conmorfeusz; + morfeusz = self.packages.${system}.morfeusz; + concraft-pl = self.packages.${system}.concraft-pl; + conmorfeusz = self.packages.${system}.conmorfeusz; + + serverConfig = cfg.server.config // {port = toString cfg.server.port;}; +in { + options.services.conmorfeusz = with lib; { + enable = mkEnableOption "conmorfeusz"; + + model = mkOption { + type = types.nullOr types.package; + description = "The target concraft-pl model"; + default = null; + }; + + server = { + enable = mkEnableOption "concraft-pl server"; + port = mkOption { + type = types.port; + description = "Port on which server should listen"; + default = 8080; + }; + + config = mkOption { + type = types.attrs; + description = "CLI parameters of 'concraft-pl server' command"; + default = { + inmodel = toString (pkgs.callPackage ./concraft-sgjp-model.nix {}); + }; + }; + }; + }; + + config = lib.mkIf cfg.enable { + environment.systemPackages = [ + morfeusz + concraft-pl + (conmorfeusz.override { + model = cfg.model; + local = !cfg.server.enable; + serverUrl = + if cfg.server.enable + then "http://localhost:${toString cfg.server.port}" + else null; + }) + ]; + + systemd.services.conmorfeusz = lib.mkIf cfg.server.enable { + enable = true; + description = "Conmorfeusz"; + + wantedBy = ["multi-user.target"]; + + serviceConfig = { + ExecStart = "${concraft-pl}/bin/concraft-pl server ${lib.cli.toGNUCommandLineShell {} serverConfig}"; + }; + }; + }; }