flake.nix 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. {
  2. inputs = {
  3. nixpkgs.url = github:NixOS/nixpkgs/nixos-unstable;
  4. flake-utils.url = github:numtide/flake-utils;
  5. drawio2py.url = git+ssh://git@msdl.uantwerpen.be/jexelmans/drawio2py.git?ref=library;
  6. xopp2py.url = git+ssh://git@msdl.uantwerpen.be/jexelmans/xopp2py.git;
  7. drawio.url = git+ssh://git@msdl.uantwerpen.be/jexelmans/drawio.git;
  8. wee.url = git+ssh://git@msdl.uantwerpen.be/lucasalbertins/wee.git;
  9. };
  10. outputs = { self, nixpkgs, flake-utils, drawio2py, xopp2py, drawio, wee }:
  11. flake-utils.lib.eachDefaultSystem (system:
  12. let
  13. pkgs = nixpkgs.legacyPackages.${system};
  14. myLibs = {
  15. drawio2py = drawio2py.defaultPackage.${system};
  16. xopp2py = xopp2py.defaultPackage.${system};
  17. };
  18. drawioWebRoot = drawio.packages.${system}.webRoot;
  19. in {
  20. packages = rec {
  21. # Python library
  22. drawio2oml =
  23. let
  24. in
  25. pkgs.python3Packages.buildPythonPackage {
  26. pname = "drawio2oml";
  27. version = "1.0.0";
  28. src = ./.; # location of setup.py
  29. propagatedBuildInputs = [
  30. myLibs.drawio2py
  31. pkgs.python3Packages.jinja2
  32. pkgs.python3Packages.requests
  33. ];
  34. checkPhase = ''
  35. runHook preCheck
  36. DRAWIOSHAPELIBS="${drawioWebRoot}/share/myPlugins/shape_libs" ${pkgs.python3}/bin/python -m unittest test.run_tests
  37. runHook postCheck
  38. '';
  39. };
  40. # The backend script - to be called with PROJECTDIR arguments
  41. backend =
  42. let
  43. # By default, gradle depends on 'full java'. For our purposes, this is not necessary:
  44. gradleHeadless = pkgs.gradle.override { java=pkgs.jdk19_headless; };
  45. in
  46. pkgs.python3Packages.buildPythonApplication {
  47. pname = "dtdesign-backend";
  48. version = "1.0.0";
  49. src = ./backend; # location of setup.py
  50. makeWrapperArgs = [
  51. "--set PATH \\$PATH:${gradleHeadless}/bin/"
  52. "--add-flags ${drawioWebRoot}/share/myPlugins/shape_libs"
  53. ];
  54. propagatedBuildInputs = [
  55. myLibs.drawio2py
  56. myLibs.xopp2py
  57. drawio2oml
  58. pkgs.python3Packages.flask
  59. pkgs.python3Packages.flask-cors
  60. ];
  61. };
  62. # Service monitor - PROJECTDIR needs to be passed as environment variable
  63. # Runs 4 services:
  64. # - Fuseki
  65. # - Lighttpd
  66. # - backend script (defined above)
  67. # - Workflow Enactment Engine (Wee™)
  68. serviceMonitor =
  69. let
  70. # Build the latest Fuseki version, and tell it to use headless Java:
  71. fusekiHeadless = (import ./backend/fuseki.nix) { pkgs=pkgs; java=pkgs.jdk19_headless; };
  72. drawioStaticServer = drawio.packages.${system}.staticServer;
  73. weeServer = wee.packages.${system}.wee;
  74. commonSettings = ''
  75. stdout_logfile=/dev/stdout
  76. stdout_logfile_maxbytes=0
  77. redirect_stderr=true
  78. startretries=1
  79. killasgroup=true
  80. '';
  81. supervisordConfigFile = pkgs.writeTextFile {
  82. name = "supervisord.conf";
  83. text = ''
  84. [supervisord]
  85. nodaemon=true
  86. logfile_maxbytes=0
  87. [program:fuseki]
  88. directory=%(ENV_PROJECTDIR)s/.fuseki
  89. command=${fusekiHeadless}/bin/fuseki run
  90. environment=FUSEKI_BASE="%(ENV_PROJECTDIR)s/.fuseki/run",PATH="${pkgs.which}/bin:${pkgs.coreutils}/bin:%(ENV_PATH)s",FUSEKI_CONF="%(ENV_PROJECTDIR)s/.fuseki.ttl"
  91. ${commonSettings}
  92. [program:lighttpd]
  93. command=${drawioStaticServer}/bin/drawio-msdl-staticserver
  94. ${commonSettings}
  95. [program:backend]
  96. command=${backend}/bin/dtdesign-backend %(ENV_PROJECTDIR)s
  97. ${commonSettings}
  98. [program:wee]
  99. command=${weeServer}/bin/wee
  100. ${commonSettings}
  101. '';
  102. };
  103. runner = pkgs.writeShellScript "dtdesign_service_monitor.sh" ''
  104. if [[ -z "''${PROJECTDIR}" ]]; then
  105. echo "Error: Please set the environment variable PROJECTDIR to the 'SystemDesignOntology2Layers' directory."
  106. exit 1
  107. fi
  108. exec ${pkgs.python3Packages.supervisor}/bin/supervisord -c ${supervisordConfigFile}
  109. '';
  110. in
  111. pkgs.stdenv.mkDerivation {
  112. name = "dtdesign-service-monitor";
  113. src = ./.;
  114. installPhase = ''
  115. mkdir -p $out/bin
  116. cp ${runner} $out/bin/serviceMonitor
  117. '';
  118. };
  119. # Docker container - no dependencies (contains its own PROJECTDIR clone)
  120. dockerContainer =
  121. let
  122. dtdesignRepo = builtins.fetchGit {
  123. url = "git@msdl.uantwerpen.be:lucasalbertins/DTDesign.git";
  124. rev = "795cb854077d82fa6370ad64b8b6f06530e11bbc";
  125. ref = "main";
  126. shallow = true;
  127. };
  128. in pkgs.dockerTools.buildImage {
  129. name = "dtdesign-backend";
  130. runAsRoot = ''
  131. #!${pkgs.runtimeShell}
  132. mkdir /tmp
  133. cp -r ${dtdesignRepo}/examples/oml/SystemDesignOntology2Layers /projectdir
  134. '';
  135. config = {
  136. Env = [ "PROJECTDIR=/projectdir" ]; # tell the script where the projectdir is at.
  137. Cmd = [ "${serviceMonitor}" ]; # ran when container starts
  138. ExposedPorts = {
  139. "5000/tcp" = {}; # python backend
  140. "3030/tcp" = {}; # fuseki
  141. "8700/tcp" = {}; # lightttp (serving drawio)
  142. };
  143. Volumes = {
  144. "/projectdir" = {}; # path to persist as Docker 'Volume'
  145. };
  146. };
  147. };
  148. default = drawio2oml;
  149. };
  150. apps = rec {
  151. # App that starts when invoking `nix run .#serviceMonitor`
  152. serviceMonitor = {
  153. type = "app";
  154. program = "${self.packages.${system}.serviceMonitor}/bin/serviceMonitor";
  155. };
  156. # App that starts when invoking `nix run .`
  157. default = serviceMonitor;
  158. };
  159. });
  160. nixConfig.bash-prompt-prefix = "\\e\[94;1m[drawio2oml]\\e\[m ";
  161. }