Personal emacs config
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
4.7 KiB

  1. ;;; nixos-options.el --- Interface for browsing and completing NixOS options.
  2. ;; Copyright (C) 2015 Diego Berrocal and Travis B. Hartwell
  3. ;; Author: Diego Berrocal <cestdiego@gmail.com>
  4. ;; Travis B. Hartwell <nafai@travishartwell.net>
  5. ;; Created: 18 July 2015
  6. ;; Keywords: unix
  7. ;; Package-Version: 0.0.1
  8. ;; Package-Commit: 5fc8fa29bea9dd8e9c822af92f9bc6ddc223635f
  9. ;; Homepage: http://www.github.com/travisbhartwell/nix-emacs/
  10. ;; Version: 0.0.1
  11. ;; Package-Requires: ((emacs "24") (json "1.4"))
  12. ;; This file is not part of GNU Emacs.
  13. ;;; License: GPLv3
  14. ;;; Commentary:
  15. ;; Useful functions for exploring the NixOS options. Inspired by
  16. ;; https://nixos.org/nixos/options.html.
  17. ;;; Code:
  18. (require 'json)
  19. (defvar nixos-options-name-indent-amount 0
  20. "Indent by the maximum length, plus a colon, plus two spaces.")
  21. ;; Macros for defining constants and functions for working with options
  22. (defmacro define-nixos-options-item (item long-name)
  23. (let* ((name-const (intern (concat "nixos-options-" item)))
  24. (long-name-const (intern (concat "nixos-options-" item "-long-name")))
  25. (long-name-length-plus-padding (+ 3 (length long-name)))
  26. (long-name-docstring (format "The long description for %s." item))
  27. (item-getter (intern (concat "nixos-options-get-" item)))
  28. (item-getter-docstring
  29. (format "Get the value of %s from OPTION." item))
  30. (item-display (intern (concat "nixos-options-display-" item)))
  31. (item-display-docstring
  32. (format "Display the value for %s from OPTION." item)))
  33. `(progn
  34. (defconst ,name-const ,item)
  35. (defconst ,long-name-const ,long-name ,long-name-docstring)
  36. (if (> ,long-name-length-plus-padding nixos-options-name-indent-amount)
  37. (setq nixos-options-name-indent-amount
  38. ,long-name-length-plus-padding))
  39. (defun ,item-getter (option)
  40. ,item-getter-docstring
  41. (cdr (assoc ,name-const option)))
  42. (defun ,item-display (option)
  43. ,item-display-docstring
  44. (let ((item (,item-getter option))
  45. (format-string
  46. (format "%%-%ds %%s\n" nixos-options-name-indent-amount)))
  47. (if (not (null item))
  48. (format format-string (concat ,long-name-const ":") item)
  49. ""))))))
  50. (define-nixos-options-item "name" "Name")
  51. (define-nixos-options-item "type" "Type")
  52. (define-nixos-options-item "description" "Description")
  53. (define-nixos-options-item "default" "Default value")
  54. (define-nixos-options-item "example" "Example value")
  55. (define-nixos-options-item "declarations" "Declared in")
  56. (defvar nixos-options-json-file
  57. (let* ((cmd
  58. "nix-build --no-out-link '<nixpkgs/nixos/release.nix>' -A options")
  59. (dir (replace-regexp-in-string "\n\\'" ""
  60. (shell-command-to-string cmd))))
  61. (expand-file-name "share/doc/nixos/options.json" dir))
  62. "Location of the options file.")
  63. (defun nixos-options--boolean-string (value)
  64. "Return the string representation of the boolean VALUE.
  65. Returns VALUE unchanged if not a boolean."
  66. (cond ((eq value 't) "true")
  67. ((eq value :json-false) "false")
  68. (t value)))
  69. (defun nixos-options--make-alist (option)
  70. (let ((name (car option))
  71. (data (cdr option))
  72. (default (nixos-options-get-default option))
  73. (example (nixos-options-get-example option)))
  74. (progn
  75. (if (not (null default))
  76. (setcdr (assoc nixos-options-default option)
  77. (nixos-options--boolean-string default)))
  78. (if (not (null example))
  79. (setcdr (assoc nixos-options-example option)
  80. (nixos-options--boolean-string example)))
  81. (add-to-list 'data `(,nixos-options-name . ,name))
  82. `(,name . ,data))))
  83. (defvar nixos-options
  84. (let* ((json-key-type 'string)
  85. (raw-options (json-read-file nixos-options-json-file)))
  86. (mapcar 'nixos-options--make-alist raw-options)))
  87. (defun nixos-options-get-documentation-for-option (option)
  88. (concat (nixos-options-display-name option)
  89. (nixos-options-display-type option)
  90. (nixos-options-display-description option)
  91. (nixos-options-display-default option)
  92. (nixos-options-display-example option)
  93. (nixos-options-display-declarations option)))
  94. ;; Borrowed from anaconda-mode
  95. (defun nixos-options-doc-buffer (doc)
  96. "Display documentation buffer with contents DOC."
  97. (let ((buf (get-buffer-create "*nixos-options-doc*")))
  98. (with-current-buffer buf
  99. (view-mode -1)
  100. (erase-buffer)
  101. (insert doc)
  102. (goto-char (point-min))
  103. (view-mode 1)
  104. buf)))
  105. (defun nixos-options-get-option-by-name (name)
  106. (assoc name nixos-options))
  107. (provide 'nixos-options)
  108. ;;; nixos-options.el ends here