generate the requires list and docstrings via nix and unit module

This commit is contained in:
Sameer Rahmani 2024-04-15 18:57:22 +01:00
parent 74d36d5ae1
commit 92549b9a08
Signed by: lxsameer
GPG Key ID: 8741FACBF412FFA5
5 changed files with 89 additions and 49 deletions

View File

@ -26,6 +26,8 @@
;; ;;
;; Please note that this file runs with Emacs and not FG42. ;; Please note that this file runs with Emacs and not FG42.
;;; Code: ;;; Code:
(require 'loaddefs-gen)
(when (not (json-available-p)) (when (not (json-available-p))
(error "Error: libjasson support is missing")) (error "Error: libjasson support is missing"))
@ -60,6 +62,9 @@
;; ============================================================================ ;; ============================================================================
;; This file is auto generated by FG42. Don't modify it. ;; This file is auto generated by FG42. Don't modify it.
;; ============================================================================ ;; ============================================================================
;; Since it is deprecated we can easily avoide other packages to use it
(provide 'cl)
") ")
(defvar file-footer " (defvar file-footer "
@ -68,17 +73,21 @@
;; fg42_init.el ends here ;; fg42_init.el ends here
") ")
(defvar keys-to-skip '("docstrings" "mimeTypes"))
;; Elisp's hashtables are not functional so I have to store the ;; Elisp's hashtables are not functional so I have to store the
;; state like this, Ewwwwww! ;; state like this, Ewwwwww!
(defvar build-state '() (defvar build-state '()
"A list of expressions that will dump in the output at the end.") "A list of expressions that will dump in the output at the end.")
(defvar build-output (cadr command-line-args-left))
;;; Build related stuff
(defmacro with-json (var &rest body) (defmacro with-json (var &rest body)
"Read the json file from cli arguments, user VAR run the BODY on it." "Read the json file from cli arguments, user VAR run the BODY on it."
(declare (indent defun)) (declare (indent defun))
`(let ((input-file (car command-line-args-left))) `(let ((input-file (car command-line-args-left)))
(message "Reading the input file: %s" input-file) (message "[build.el]: Reading the input file: %s" input-file)
(with-temp-buffer (with-temp-buffer
(insert-file-contents input-file) (insert-file-contents input-file)
(let ((,var (json-parse-buffer))) (let ((,var (json-parse-buffer)))
@ -94,24 +103,29 @@
,@(mapcar (lambda (pkg) `(require ',(intern pkg))) elisp-pkgs))))) ,@(mapcar (lambda (pkg) `(require ',(intern pkg))) elisp-pkgs)))))
(defun generate-generic-vars (k v) (defun generate-generic-vars (k v doc)
"Generate a list of vars for K and value V." "Generate a list of vars for K and value V with docstring DOC."
(add-to-list 'build-state `(defvar ,(intern (format "fg42/config/%s" k)) ,v))) (add-to-list 'build-state `(defvar ,(intern (format "fg42/config/%s" k)) ,v ,doc)))
(defun fg42-config-key-handler (k v) (defun fg42-config-key-handler (config)
"Handle the FG42 config K and its value V." "Generate a lambda to process key/value pairs in CONFIG."
(cond (lambda (k v)
((string= k "requires") "Handle the FG42 config K and its value V."
(generate-compile-time-requires v)) (cond
((string= k "requires")
(t (generate-generic-vars k v)))) (generate-compile-time-requires v))
((member k keys-to-skip)
(message "[build.el]: Skipping %s..." k))
(t (generate-generic-vars k v (gethash k (gethash "docstrings" config)))))))
(defun fg42-handler (config) (defun fg42-handler (config)
"Handle the CONFIG for FG42. "Handle the CONFIG for FG42.
CONFIG maps to the collective `config' value of Nix modules." CONFIG maps to the collective `config' value of Nix modules."
(maphash #'fg42-config-key-handler config)) (maphash (fg42-config-key-handler config) config))
(defun handle-conifg-for (k v) (defun handle-conifg-for (k v)
"Find the handler of K and its value V to it." "Find the handler of K and its value V to it."
@ -124,15 +138,17 @@ CONFIG maps to the collective `config' value of Nix modules."
(error "Don't know how to handle '%s'" k)) (error "Don't know how to handle '%s'" k))
(handle-conifg-for k v)) (handle-conifg-for k v))
(with-json j (with-json j
;; TODO: To make it scale, may be let Nix modules ;; TODO: To make it scale, may be let Nix modules
;; plug into this script. There is a security ;; plug into this script. There is a security
;; rist though ;; rist though
(maphash #'handle-top-level j) (maphash #'handle-top-level j)
(with-temp-file (cadr command-line-args-left) (with-temp-file build-output
(insert file-header) (insert file-header)
(mapc (lambda (x) (print x (current-buffer))) build-state) (mapc (lambda (x) (print x (current-buffer))) build-state)
(insert file-footer))) (insert file-footer))
(message "[build.el]: Done!"))
(provide 'build) (provide 'build)

View File

@ -18,8 +18,6 @@
# from FG42 modules that are structurally the same but used in # from FG42 modules that are structurally the same but used in
# different context # different context
{ pkgs, lib ? pkgs.lib, modules ? null, extraModules ? [ ] }: { pkgs, lib ? pkgs.lib, modules ? null, extraModules ? [ ] }:
with lib;
let let
fg42Modules = fg42Modules =
if modules != null if modules != null
@ -33,9 +31,8 @@ let
utils = pkgs.callPackage ./utils.nix { }; utils = pkgs.callPackage ./utils.nix { };
unitModule = import ./unit.nix { inherit pkgs lib; };
final = lib.evalModules { final = lib.evalModules {
modules = [ unitModule ] ++ fg42Modules ++ extraModules; modules = [ ./unit.nix ] ++ fg42Modules ++ extraModules;
specialArgs = { specialArgs = {
inherit utils; inherit utils;
inherit (utils) makeFG42Drv; inherit (utils) makeFG42Drv;

View File

@ -86,8 +86,8 @@ stdenv.mkDerivation rec {
chmod 755 $LISPDIR -R chmod 755 $LISPDIR -R
emacs --batch -l $LISPDIR/lisp/build.el $LISPDIR/config.json $STARTUP_FILE emacs --batch -l $LISPDIR/lisp/build.el $LISPDIR/config.json $STARTUP_FILE
#emacs -Q --batch -l package --eval "(package-generate-autoloads \"fg42_init" \"$LISPDIR\")"
emacs --batch -l loaddefs-gen -f loaddefs-generate-batch $LISPDIR/lisp/ $LISPDIR/lisp/ ${emacsBundle}
cat >> $out/share/applications/FG42.desktop << EOF cat >> $out/share/applications/FG42.desktop << EOF
[Desktop Entry] [Desktop Entry]

View File

@ -17,8 +17,11 @@
# This is the home manager module that exposes FG42. It differs # This is the home manager module that exposes FG42. It differs
# from FG42 modules that are structurally the same but used in # from FG42 modules that are structurally the same but used in
# different context # different context
{ lib, pkgs, ... }: { lib, pkgs, config, options, ... }:
with lib; with lib;
let
nix2elispName = pkg: (builtins.replaceStrings [ "fg42-" ] [ "fg42/" ] pkg.pname);
in
{ {
options = { options = {
fg42.version = mkOption { fg42.version = mkOption {
@ -44,17 +47,6 @@ with lib;
''; '';
}; };
fg42.requires = mkOption {
type = types.listOf types.str;
default = [ ];
description = ''
A list of Emacs packages to preload in compile time.
In general you want your entry point module in this list
to setup your autoloads, hooks, and everything.
'';
};
fg42.paths = mkOption { fg42.paths = mkOption {
type = types.listOf types.package; type = types.listOf types.package;
default = [ ]; default = [ ];
@ -91,6 +83,7 @@ with lib;
type = types.str; type = types.str;
description = "The theme name to use with FG42."; description = "The theme name to use with FG42.";
}; };
fg42.theme-package-name = mkOption fg42.theme-package-name = mkOption
{ {
type = types.str; type = types.str;
@ -100,10 +93,46 @@ with lib;
''; '';
}; };
fg42.requires = mkOption {
type = types.listOf types.str;
readOnly = true;
description = ''
A list of Emacs packages to preload in compile time.
In general you want your entry point module in this list
to setup your autoloads, hooks, and everything.
'';
};
fg42.docstrings = mkOption {
type = types.attrsOf types.str;
readOnly = true;
description = ''
A list of Emacs packages to preload in compile time.
In general you want your entry point module in this list
to setup your autoloads, hooks, and everything.
'';
};
}; };
config = { config = {
fg42.version = import ../version.nix { }; fg42.version = import ../version.nix { };
# Extract the package pname's to pass to elisp to `require` them in the init
# file, while compiling.
fg42.requires = map nix2elispName config.fg42.elispPackages;
fg42.docstrings = (with builtins;
let
desc = opt:
if hasAttr "description" opt
then opt.description
else "";
in
lib.concatMapAttrs
(k: v: { ${k} = desc v; })
options.fg42);
}; };
} }

View File

@ -41,7 +41,7 @@ let
base16-theme base16-theme
]); ]);
drv = makeFG42Drv { drv = makeFG42Drv {
pname = "fg42-editor"; pname = "editor";
version = config.fg42.version; version = config.fg42.version;
buildInputs = deps; buildInputs = deps;
src = ./.; src = ./.;
@ -50,23 +50,21 @@ let
dicts = pkgs.aspellWithDicts (dicts: with dicts; [ en en-computers en-science ]); dicts = pkgs.aspellWithDicts (dicts: with dicts; [ en en-computers en-science ]);
in in
{ {
config = { fg42.elispPackages = [ drv ] ++ deps;
fg42.elispPackages = [ drv ] ++ deps;
fg42.fonts = (with pkgs;[ fg42.fonts = (with pkgs;[
vazir-fonts vazir-fonts
fira-code fira-code
fira-mono fira-mono
noto-fonts noto-fonts
]); ]);
fg42.paths = (with pkgs;[ fg42.paths = (with pkgs;[
ripgrep ripgrep
dicts dicts
]); ]);
fg42.font = lib.mkDefault '''("Fira Mono" 11)''; fg42.font = lib.mkDefault '''("Fira Mono" 11)'';
fg42.theme = lib.mkDefault "base16-eighties"; fg42.theme = lib.mkDefault "base16-eighties";
fg42.theme-package-name = lib.mkDefault "base16-theme"; fg42.theme-package-name = lib.mkDefault "base16-theme";
};
} }