From 78db43898b2d1134a063e57300ee470cd1b8d1be Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Wed, 22 Jan 2020 01:27:21 +0000 Subject: [PATCH] refactor(fun/gemma): Use buildLisp.nix to build Gemma This removes the ASDF system definition for Gemma and switches the code over to buildLisp. The program builds (including some terrifying hacks to get the frontend to work), but there are some bizarre runtime issues that I need to debug. --- fun/gemma/build.lisp | 5 --- fun/gemma/default.nix | 73 +++++++++++++++++----------------------- fun/gemma/gemma.asd | 33 ------------------ fun/gemma/src/gemma.lisp | 21 +++++------- third_party/default.nix | 1 + 5 files changed, 41 insertions(+), 92 deletions(-) delete mode 100644 fun/gemma/build.lisp delete mode 100644 fun/gemma/gemma.asd diff --git a/fun/gemma/build.lisp b/fun/gemma/build.lisp deleted file mode 100644 index e935ce25f..000000000 --- a/fun/gemma/build.lisp +++ /dev/null @@ -1,5 +0,0 @@ -(require :asdf) -(require :sb-posix) - -(push (format nil "~A/" (sb-posix:getcwd)) asdf:*central-registry*) -(asdf:operate 'asdf:program-op :gemma) diff --git a/fun/gemma/default.nix b/fun/gemma/default.nix index f48af4892..53b47f7cb 100644 --- a/fun/gemma/default.nix +++ b/fun/gemma/default.nix @@ -1,61 +1,50 @@ { pkgs, ... }: let - inherit (pkgs) elmPackages lispPackages; - inherit (pkgs.third_party) stdenv sbcl makeWrapper openssl; + inherit (pkgs) elmPackages; + inherit (pkgs.third_party) cacert iana-etc libredirect stdenv runCommandNoCC; frontend = stdenv.mkDerivation { - name = "gemma-frontend"; + name = "gemma-frontend.html"; src = ./frontend; - buildInputs = [ elmPackages.elm ]; + buildInputs = [ cacert iana-etc elmPackages.elm ]; + + # The individual Elm packages this requires are not packaged and I + # can't be bothered to do that now, so lets open the escape hatch: + outputHashAlgo = "sha256"; + outputHash = "000xhds5bsig3kbi7dhgbv9h7myacf34bqvw7avvz7m5mwnqlqg7"; phases = [ "unpackPhase" "buildPhase" ]; buildPhase = '' + export NIX_REDIRECTS=/etc/protocols=${iana-etc}/etc/protocols \ + LD_PRELOAD=${libredirect}/lib/libredirect.so + + export SYSTEM_CERTIFICATE_PATH=${cacert}/etc/ssl/certs + mkdir .home && export HOME="$PWD/.home" - mkdir -p $out - elm-make --yes Main.elm --output $out/index.html + elm-make --yes Main.elm --output $out ''; }; -in stdenv.mkDerivation rec { - name = "gemma"; - src = ./.; - nativeBuildInputs = with lispPackages; [ - sbcl - hunchentoot + injectFrontend = pkgs.writeText "gemma-frontend.lisp" '' + (in-package :gemma) + (setq *static-file-location* "${runCommandNoCC "frontend" {} '' + mkdir -p $out + cp ${frontend} $out/index.html + ''}}") + ''; +in pkgs.nix.buildLisp.program { + name = "gemma"; + + deps = with pkgs.third_party.lisp; [ cl-json - cffi cl-prevalence + hunchentoot local-time - makeWrapper ]; - buildPhase = '' - mkdir -p $out/share/gemma $out/bin - - # Build Lisp using the Nix-provided wrapper which sets the load - # paths correctly. - cd $src - env GEMMA_BIN_TARGET=$out/bin/gemma common-lisp.sh --load build.lisp - - # Wrap gemma to find OpenSSL at runtime: - wrapProgram $out/bin/gemma --prefix LD_LIBRARY_PATH : "${openssl.out}/lib" - - # and finally copy the frontend to the appropriate spot - cp ${frontend}/index.html $out/share/gemma/index.html - ''; - - installPhase = "true"; - - # Stripping an SBCL executable removes the application, which is unfortunate. - dontStrip = true; - - meta = with stdenv.lib; { - description = "Tool for tracking recurring tasks"; - homepage = "https://github.com/tazjin/gemma"; - license = licenses.gpl3; - - # Lisp builds are broken for some reason (2019-09-22) - broken = true; - }; + srcs = [ + ./src/gemma.lisp + injectFrontend + ]; } diff --git a/fun/gemma/gemma.asd b/fun/gemma/gemma.asd deleted file mode 100644 index 0aea9c95f..000000000 --- a/fun/gemma/gemma.asd +++ /dev/null @@ -1,33 +0,0 @@ -#| - This file is part of Gemma. - - Gemma is free software: you can redistribute it and/or modify it - under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - Copyright (c) 2017 Vincent Ambo -|# - -(require 'sb-posix) - -(defsystem "gemma" - :version "0.1.0" - :author "Vincent Ambo" - :license "GPLv3" - :depends-on (local-time - hunchentoot - cl-json - cl-prevalence) - :components ((:module "src" - :components - ((:file "gemma")))) - :build-operation program-op - :build-pathname #.(or (pathname (sb-posix:getenv "GEMMA_BIN_TARGET")) - "gemma") - :entry-point "gemma::entrypoint" - :description "Gemma is a household task management system" - :long-description - #.(read-file-string - (subpathname *load-pathname* "README.markdown")) - :in-order-to ((test-op (test-op "gemma-test")))) diff --git a/fun/gemma/src/gemma.lisp b/fun/gemma/src/gemma.lisp index b8a20addd..cf074cfb8 100644 --- a/fun/gemma/src/gemma.lisp +++ b/fun/gemma/src/gemma.lisp @@ -1,4 +1,4 @@ -;; Copyright (C) 2016-2017 Vincent Ambo +;; Copyright (C) 2016-2020 Vincent Ambo ;; ;; This file is part of Gemma. ;; @@ -13,7 +13,7 @@ :cl-json) (:import-from :sb-posix :getenv) (:shadowing-import-from :sb-posix :getcwd) - (:export :start-gemma :config :entrypoint)) + (:export :start-gemma :config :main)) (in-package :gemma) ;; TODO: Store an average of how many days it was between task @@ -29,18 +29,15 @@ (defvar *gemma-port* 4242 "Port on which the Gemma web server listens.") -(defvar *static-file-location* - (or (in-case-of (sb-posix:getenv "out") - (concatenate 'string it "/share/gemma/")) - "frontend/") +(defvar *static-file-location* "frontend/" "Folder from which to serve static assets. If built inside of Nix, - the folder is concatenated with the output path at which the files - are expected to be.") + the path is injected during the build.") + +(defvar *p-tasks* nil + "All tasks registered in this Gemma instance.") (defun initialise-persistence (data-dir) - (defvar *p-tasks* - (cl-prevalence:make-prevalence-system data-dir) - "All tasks registered in this Gemma instance.") + (setq *p-tasks* (cl-prevalence:make-prevalence-system data-dir)) ;; Initialise database ID counter (or (> (length (cl-prevalence:find-all-objects *p-tasks* 'task)) 0) @@ -161,7 +158,7 @@ maximum interval." (complete-task key) (encode-json-to-string (response-for (get-task key)))))) -(defun entrypoint () +(defun main () "This function serves as the entrypoint for ASDF-built executables. It joins the Hunchentoot server thread to keep the process running for as long as the server is alive." diff --git a/third_party/default.nix b/third_party/default.nix index 1df13abd2..fca4ce94c 100644 --- a/third_party/default.nix +++ b/third_party/default.nix @@ -54,6 +54,7 @@ let jq kontemplate lib + libredirect lispPackages llvmPackages luajit