Compare commits

...

16 Commits

Author SHA1 Message Date
atagen
451a384b97 add license 2025-04-25 11:33:59 +10:00
atagen
202900e22a docs nit 2025-03-10 17:32:01 +11:00
atagen
48e77e9585 add shell monitoring 2025-02-26 16:06:28 +11:00
atagen
d0532b9cc9 quieten sd commands 2025-02-26 16:06:00 +11:00
atagen
97cd54cc50 overhaul commands 2025-02-26 16:05:30 +11:00
atagen
caa156daf4 modularise ides workings 2025-02-07 15:12:17 +11:00
atagen
9248cc0945 fix ignoring inline serviceDefs
inherit nit
2025-02-07 15:06:50 +11:00
atagen
242343a839 update docs 2025-02-06 11:29:31 +11:00
atagen
ecb61f146f update redis module to allow multiple instances 2025-02-06 11:29:22 +11:00
atagen
1d3181f16e add individual service management 2025-02-06 11:29:09 +11:00
atagen
32151a99d1 make autorun optional
squash
2025-02-06 11:28:16 +11:00
atagen
9a8c417cbb improve docs and comments 2025-02-06 11:25:02 +11:00
atagen
3b6c83d580 create socket+path+timer options 2025-02-06 11:23:52 +11:00
atagen
163fae5498 fix flake lib output 2025-02-06 11:20:27 +11:00
atagen
01135c8d14 move docs 2025-02-02 18:41:24 +11:00
atagen
78eda8579b deadnix/statix 2025-02-02 18:36:12 +11:00
15 changed files with 1135 additions and 376 deletions

156
LICENSE Normal file
View File

@ -0,0 +1,156 @@
EUROPEAN UNION PUBLIC LICENCE v. 1.2
EUPL © the European Union 2007, 2016
This European Union Public Licence (the EUPL) applies to the Work (as defined below) which is provided under the terms of this Licence. Any use of the Work, other than as authorised under this Licence is prohibited (to the extent such use is covered by a right of the copyright holder of the Work).
The Work is provided under the terms of this Licence when the Licensor (as defined below) has placed the following notice immediately following the copyright notice for the Work:
Licensed under the EUPL
or has expressed by any other means his willingness to license under the EUPL.
1. Definitions
In this Licence, the following terms have the following meaning:
The Licence: this Licence.
The Original Work: the work or software distributed or communicated by the Licensor under this Licence, available as Source Code and also as Executable Code as the case may be.
Derivative Works: the works or software that could be created by the Licensee, based upon the Original Work or modifications thereof. This Licence does not define the extent of modification or dependence on the Original Work required in order to classify a work as a Derivative Work; this extent is determined by copyright law applicable in the country mentioned in Article 15.
The Work: the Original Work or its Derivative Works.
The Source Code: the human-readable form of the Work which is the most convenient for people to study and modify.
The Executable Code: any code which has generally been compiled and which is meant to be interpreted by a computer as a program.
The Licensor: the natural or legal person that distributes or communicates the Work under the Licence.
Contributor(s): any natural or legal person who modifies the Work under the Licence, or otherwise contributes to the creation of a Derivative Work.
The Licensee or You: any natural or legal person who makes any usage of the Work under the terms of the Licence.
Distribution or Communication: any act of selling, giving, lending, renting, distributing, communicating, transmitting, or otherwise making available, online or offline, copies of the Work or providing access to its essential functionalities at the disposal of any other natural or legal person.
2. Scope of the rights granted by the Licence
The Licensor hereby grants You a worldwide, royalty-free, non-exclusive, sublicensable licence to do the following, for the duration of copyright vested in the Original Work:
— use the Work in any circumstance and for all usage,
— reproduce the Work,
— modify the Work, and make Derivative Works based upon the Work,
— communicate to the public, including the right to make available or display the Work or copies thereof to the public and perform publicly, as the case may be, the Work,
— distribute the Work or copies thereof,
— lend and rent the Work or copies thereof,
— sublicense rights in the Work or copies thereof.
Those rights can be exercised on any media, supports and formats, whether now known or later invented, as far as the applicable law permits so.
In the countries where moral rights apply, the Licensor waives his right to exercise his moral right to the extent allowed by law in order to make effective the licence of the economic rights here above listed.
The Licensor grants to the Licensee royalty-free, non-exclusive usage rights to any patents held by the Licensor, to the extent necessary to make use of the rights granted on the Work under this Licence.
3. Communication of the Source Code
The Licensor may provide the Work either in its Source Code form, or as Executable Code. If the Work is provided as Executable Code, the Licensor provides in addition a machine-readable copy of the Source Code of the Work along with each copy of the Work that the Licensor distributes or indicates, in a notice following the copyright notice attached to the Work, a repository where the Source Code is easily and freely accessible for as long as the Licensor continues to distribute or communicate the Work.
4. Limitations on copyright
Nothing in this Licence is intended to deprive the Licensee of the benefits from any exception or limitation to the exclusive rights of the rights owners in the Work, of the exhaustion of those rights or of other applicable limitations thereto.
5. Obligations of the Licensee
The grant of the rights mentioned above is subject to some restrictions and obligations imposed on the Licensee. Those obligations are the following:
Attribution right: The Licensee shall keep intact all copyright, patent or trademarks notices and all notices that refer to the Licence and to the disclaimer of warranties. The Licensee must include a copy of such notices and a copy of the Licence with every copy of the Work he/she distributes or communicates. The Licensee must cause any Derivative Work to carry prominent notices stating that the Work has been modified and the date of modification.
Copyleft clause: If the Licensee distributes or communicates copies of the Original Works or Derivative Works, this Distribution or Communication will be done under the terms of this Licence or of a later version of this Licence unless the Original Work is expressly distributed only under this version of the Licence — for example by communicating EUPL v. 1.2 only. The Licensee (becoming Licensor) cannot offer or impose any additional terms or conditions on the Work or Derivative Work that alter or restrict the terms of the Licence.
Compatibility clause: If the Licensee Distributes or Communicates Derivative Works or copies thereof based upon both the Work and another work licensed under a Compatible Licence, this Distribution or Communication can be done under the terms of this Compatible Licence. For the sake of this clause, Compatible Licence refers to the licences listed in the appendix attached to this Licence. Should the Licensee's obligations under the Compatible Licence conflict with his/her obligations under this Licence, the obligations of the Compatible Licence shall prevail.
Provision of Source Code: When distributing or communicating copies of the Work, the Licensee will provide a machine-readable copy of the Source Code or indicate a repository where this Source will be easily and freely available for as long as the Licensee continues to distribute or communicate the Work.
Legal Protection: This Licence does not grant permission to use the trade names, trademarks, service marks, or names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the copyright notice.
6. Chain of Authorship
The original Licensor warrants that the copyright in the Original Work granted hereunder is owned by him/her or licensed to him/her and that he/she has the power and authority to grant the Licence.
Each Contributor warrants that the copyright in the modifications he/she brings to the Work are owned by him/her or licensed to him/her and that he/she has the power and authority to grant the Licence.
Each time You accept the Licence, the original Licensor and subsequent Contributors grant You a licence to their contri­ butions to the Work, under the terms of this Licence.
7. Disclaimer of Warranty
The Work is a work in progress, which is continuously improved by numerous Contributors. It is not a finished work and may therefore contain defects or bugs inherent to this type of development.
For the above reason, the Work is provided under the Licence on an as is basis and without warranties of any kind concerning the Work, including without limitation merchantability, fitness for a particular purpose, absence of defects or errors, accuracy, non-infringement of intellectual property rights other than copyright as stated in Article 6 of this Licence.
This disclaimer of warranty is an essential part of the Licence and a condition for the grant of any rights to the Work.
8. Disclaimer of Liability
Except in the cases of wilful misconduct or damages directly caused to natural persons, the Licensor will in no event be liable for any direct or indirect, material or moral, damages of any kind, arising out of the Licence or of the use of the Work, including without limitation, damages for loss of goodwill, work stoppage, computer failure or malfunction, loss of data or any commercial damage, even if the Licensor has been advised of the possibility of such damage. However, the Licensor will be liable under statutory product liability laws as far such laws apply to the Work.
9. Additional agreements
While distributing the Work, You may choose to conclude an additional agreement, defining obligations or services consistent with this Licence. However, if accepting obligations, You may act only on your own behalf and on your sole responsibility, not on behalf of the original Licensor or any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against such Contributor by the fact You have accepted any warranty or additional liability.
10. Acceptance of the Licence
The provisions of this Licence can be accepted by clicking on an icon I agree placed under the bottom of a window displaying the text of this Licence or by affirming consent in any other similar way, in accordance with the rules of applicable law. Clicking on that icon indicates your clear and irrevocable acceptance of this Licence and all of its terms and conditions.
Similarly, you irrevocably accept this Licence and all of its terms and conditions by exercising any rights granted to You by Article 2 of this Licence, such as the use of the Work, the creation by You of a Derivative Work or the Distribution or Communication by You of the Work or copies thereof.
11. Information to the public
In case of any Distribution or Communication of the Work by means of electronic communication by You (for example, by offering to download the Work from a remote location) the distribution channel or media (for example, a website) must at least provide to the public the information requested by the applicable law regarding the Licensor, the Licence and the way it may be accessible, concluded, stored and reproduced by the Licensee.
12. Termination of the Licence
The Licence and the rights granted hereunder will terminate automatically upon any breach by the Licensee of the terms of the Licence.
Such a termination will not terminate the licences of any person who has received the Work from the Licensee under the Licence, provided such persons remain in full compliance with the Licence.
13. Miscellaneous
Without prejudice of Article 9 above, the Licence represents the complete agreement between the Parties as to the Work.
If any provision of the Licence is invalid or unenforceable under applicable law, this will not affect the validity or enforceability of the Licence as a whole. Such provision will be construed or reformed so as necessary to make it valid and enforceable.
The European Commission may publish other linguistic versions or new versions of this Licence or updated versions of the Appendix, so far this is required and reasonable, without reducing the scope of the rights granted by the Licence. New versions of the Licence will be published with a unique version number.
All linguistic versions of this Licence, approved by the European Commission, have identical value. Parties can take advantage of the linguistic version of their choice.
14. Jurisdiction
Without prejudice to specific agreement between parties,
— any litigation resulting from the interpretation of this License, arising between the European Union institutions, bodies, offices or agencies, as a Licensor, and any Licensee, will be subject to the jurisdiction of the Court of Justice of the European Union, as laid down in article 272 of the Treaty on the Functioning of the European Union,
— any litigation arising between other parties and resulting from the interpretation of this License, will be subject to the exclusive jurisdiction of the competent court where the Licensor resides or conducts its primary business.
15. Applicable Law
Without prejudice to specific agreement between parties,
— this Licence shall be governed by the law of the European Union Member State where the Licensor has his seat, resides or has his registered office,
— this licence shall be governed by Belgian law if the Licensor has no seat, residence or registered office inside a European Union Member State.
Appendix
Compatible Licences according to Article 5 EUPL are:
— GNU General Public License (GPL) v. 2, v. 3
— GNU Affero General Public License (AGPL) v. 3
— Open Software License (OSL) v. 2.1, v. 3.0
— Eclipse Public License (EPL) v. 1.0
— CeCILL v. 2.0, v. 2.1
— Mozilla Public Licence (MPL) v. 2
— GNU Lesser General Public Licence (LGPL) v. 2.1, v. 3
— Creative Commons Attribution-ShareAlike v. 3.0 Unported (CC BY-SA 3.0) for works other than software
— European Union Public Licence (EUPL) v. 1.1, v. 1.2
— Québec Free and Open-Source Licence — Reciprocity (LiLiQ-R) or Strong Reciprocity (LiLiQ-R+)
The European Commission may update this Appendix to later versions of the above licences without producing a new version of the EUPL, as long as they provide the rights granted in Article 2 of this Licence and protect the covered Source Code from exclusive appropriation.
All other changes or additions to this Appendix require the production of a new EUPL version.

View File

@ -152,5 +152,5 @@ in case you need manual control, an ides shell provides commands:
- `restart`: do both of the above in succession
### documentation
see [module docs](docs.md)
see [module docs](docs/docs.md)

View File

@ -3,12 +3,14 @@
pkgs ? import <nixpkgs>,
shell ? pkgs.mkShell,
modules ? [ ],
auto ? true,
...
}:
# shell creation args
{
services ? { },
imports ? [ ],
serviceDefs ? { },
...
}@args:
let
@ -26,16 +28,13 @@ let
modules =
[
# ides
./ides.nix
./lib/ides.nix
# service config and build params
(
{ ... }:
{
inherit services;
_buildIdes.shellFn = shell;
_buildIdes.shellArgs = shellArgs;
}
)
(_: {
inherit services serviceDefs auto;
_buildIdes.shellFn = shell;
_buildIdes.shellArgs = shellArgs;
})
]
++ baseModules
++ modules

View File

@ -1,34 +0,0 @@
with import <nixpkgs> {};
{}: let
eval = lib.evalModules {
specialArgs = {inherit pkgs;};
modules = [./ides.nix ./modules];
};
optionsDoc = nixosOptionsDoc {
inherit (eval) options;
transformOptions = opt:
opt
// {
# Clean up declaration sites to not refer to the NixOS source tree.
declarations = let
devDir = toString /home/bolt/code/ides;
inherit (lib) hasPrefix removePrefix;
in
map
(decl:
if hasPrefix (toString devDir) (toString decl)
then let
subpath = removePrefix "/" (removePrefix (toString devDir) (toString decl));
in {
url = "https://git.atagen.co/atagen/ides/${subpath}";
name = subpath;
}
else decl)
opt.declarations;
};
};
in
runCommand "docs.md" {} ''
cat ${optionsDoc.optionsCommonMark} > $out
''

View File

@ -1,7 +1,49 @@
## auto
Whether to autostart ides services at devshell instantiation\.
*Type:*
boolean
*Default:*
` true `
*Declared by:*
- [lib/options\.nix](https://git.atagen.co/atagen/ides/lib/options.nix)
## monitor
Enable, or set timeout period for, monitoring devshell activity and automatically destroying services after (experimental)\.
*Type:*
boolean or signed integer
*Default:*
` true `
*Declared by:*
- [lib/options\.nix](https://git.atagen.co/atagen/ides/lib/options.nix)
## serviceDefs
Concrete service definitions, as per submodule options\.
Please put service-related options into ` services ` instead, and use this to implement them\.
Please put service-related options into ` options.services ` instead, and use this to implement those options\.
@ -9,7 +51,7 @@ Please put service-related options into ` services ` instead, and use this to im
attribute set of (submodule)
*Declared by:*
- [ides\.nix](https://git.atagen.co/atagen/ides/ides.nix)
- [lib/options\.nix](https://git.atagen.co/atagen/ides/lib/options.nix)
@ -35,7 +77,7 @@ string
` "run -c %CFG% --adapter caddyfile" `
*Declared by:*
- [ides\.nix](https://git.atagen.co/atagen/ides/ides.nix)
- [lib/options\.nix](https://git.atagen.co/atagen/ides/lib/options.nix)
@ -56,7 +98,7 @@ submodule
` { } `
*Declared by:*
- [ides\.nix](https://git.atagen.co/atagen/ides/ides.nix)
- [lib/options\.nix](https://git.atagen.co/atagen/ides/lib/options.nix)
@ -87,7 +129,7 @@ null or (attribute set)
```
*Declared by:*
- [ides\.nix](https://git.atagen.co/atagen/ides/ides.nix)
- [lib/options\.nix](https://git.atagen.co/atagen/ides/lib/options.nix)
@ -113,7 +155,7 @@ string
` "json" `
*Declared by:*
- [ides\.nix](https://git.atagen.co/atagen/ides/ides.nix)
- [lib/options\.nix](https://git.atagen.co/atagen/ides/lib/options.nix)
@ -126,7 +168,7 @@ Path to config file\. This overrides all other values\.
*Type:*
null or path
null or absolute path
@ -136,10 +178,10 @@ null or path
*Example:*
` /home/bolt/code/ides/configs/my-config.ini `
` "./configs/my-config.ini" `
*Declared by:*
- [ides\.nix](https://git.atagen.co/atagen/ides/ides.nix)
- [lib/options\.nix](https://git.atagen.co/atagen/ides/lib/options.nix)
@ -167,7 +209,7 @@ null or one of “java”, “json”, “yaml”, “toml”, “ini”, “xml
` "json" `
*Declared by:*
- [ides\.nix](https://git.atagen.co/atagen/ides/ides.nix)
- [lib/options\.nix](https://git.atagen.co/atagen/ides/lib/options.nix)
@ -195,7 +237,7 @@ anything
` "pkgs.formats.yaml {}.generate" `
*Declared by:*
- [ides\.nix](https://git.atagen.co/atagen/ides/ides.nix)
- [lib/options\.nix](https://git.atagen.co/atagen/ides/lib/options.nix)
@ -228,7 +270,7 @@ string
```
*Declared by:*
- [ides\.nix](https://git.atagen.co/atagen/ides/ides.nix)
- [lib/options\.nix](https://git.atagen.co/atagen/ides/lib/options.nix)
@ -254,7 +296,40 @@ string
` "caddy" `
*Declared by:*
- [ides\.nix](https://git.atagen.co/atagen/ides/ides.nix)
- [lib/options\.nix](https://git.atagen.co/atagen/ides/lib/options.nix)
## serviceDefs\.\<name>\.path
List of path options for the unit (see ` man systemd.path `) - supplied as a list due to some options allowing duplicates\.
*Type:*
attribute set of list of string
*Default:*
` { } `
*Example:*
```
{
PathModified = [
"/some/path"
];
}
```
*Declared by:*
- [lib/options\.nix](https://git.atagen.co/atagen/ides/lib/options.nix)
@ -275,7 +350,73 @@ package
` "pkgs.caddy" `
*Declared by:*
- [ides\.nix](https://git.atagen.co/atagen/ides/ides.nix)
- [lib/options\.nix](https://git.atagen.co/atagen/ides/lib/options.nix)
## serviceDefs\.\<name>\.socket
List of socket options for the unit (see ` man systemd.socket `) - supplied as a list due to some options allowing duplicates\.
*Type:*
attribute set of list of string
*Default:*
` { } `
*Example:*
```
{
ListenStream = [
"/run/user/1000/myapp.sock"
];
}
```
*Declared by:*
- [lib/options\.nix](https://git.atagen.co/atagen/ides/lib/options.nix)
## serviceDefs\.\<name>\.timer
List of timer options for the unit (see ` man systemd.path `) - supplied as a list due to some options allowing duplicates\.
*Type:*
attribute set of list of string
*Default:*
` { } `
*Example:*
```
{
OnActiveSec = [
50
];
}
```
*Declared by:*
- [lib/options\.nix](https://git.atagen.co/atagen/ides/lib/options.nix)
@ -395,6 +536,27 @@ one of “debug”, “verbose”, “notice”, “warning”, “nothing”
## services\.redis\.name
The name ides uses for this service\.
*Type:*
string
*Default:*
` "redis" `
*Declared by:*
- [modules/redis\.nix](https://git.atagen.co/atagen/ides/modules/redis.nix)
## services\.redis\.port

42
docs/docs.nix Normal file
View File

@ -0,0 +1,42 @@
with import <nixpkgs> { };
{ ... }:
let
eval = lib.evalModules {
specialArgs = { inherit pkgs; };
modules = [
../lib/ides.nix
../modules
];
};
optionsDoc = nixosOptionsDoc {
inherit (eval) options;
transformOptions =
opt:
opt
// {
# Clean up declaration sites to not refer to the NixOS source tree.
declarations =
let
devDir = toString /home/bolt/code/ides;
inherit (lib) hasPrefix removePrefix;
in
map (
decl:
if hasPrefix (toString devDir) (toString decl) then
let
subpath = removePrefix "/" (removePrefix (toString devDir) (toString decl));
in
{
url = "https://git.atagen.co/atagen/ides/${subpath}";
name = subpath;
}
else
decl
) opt.declarations;
};
};
in
runCommand "docs.md" { } ''
cat ${optionsDoc.optionsCommonMark} > $out
''

2
docs/gendocs.sh Executable file
View File

@ -0,0 +1,2 @@
#!/usr/bin/env bash
cp $(nix-build docs.nix --no-out-link) docs.md

View File

@ -1,11 +1,9 @@
{
outputs =
{ ... }:
{
lib = import ./default.nix;
templates.default = {
path = ./example;
description = "the ides template";
};
outputs = _: {
lib.use = import ./default.nix;
templates.default = {
path = ./example;
description = "the ides template";
};
};
}

268
ides.nix
View File

@ -1,268 +0,0 @@
{
pkgs,
config,
...
}:
{
#
# interface
#
options =
let
inherit (pkgs) lib;
inherit (lib) types mkOption;
serviceConfig =
with types;
submodule {
options = {
pkg = mkOption {
type = package;
description = "Package to use for service.";
example = "pkgs.caddy";
};
exec = mkOption {
type = str;
description = "Alternative executable name to use from `pkg`.";
example = "caddy";
default = "";
};
args = mkOption {
type = str;
description = "Arguments to supply to the service binary. Writing %CFG% in this will template to your config location.";
example = "run -c %CFG% --adapter caddyfile";
default = "";
};
config = mkOption {
description = "Options for setting the service's configuration.";
default = { };
type = submodule {
options = {
text = mkOption {
type = str;
default = "";
description = "Plaintext configuration to use.";
example = ''
http://*:8080 {
respond "hello"
}
'';
};
ext = mkOption {
type = str;
default = "";
description = "If your service config requires a file extension, set it here. This overrides `format`'s output path'.";
example = "json";
};
file = mkOption {
type = nullOr path;
description = "Path to config file. This overrides all other values.";
example = ./configs/my-config.ini;
default = null;
};
content = mkOption {
type = nullOr attrs;
description = "Attributes that define your config values.";
default = null;
example = {
this = "that";
};
};
format = mkOption {
type = nullOr (enum [
"java"
"json"
"yaml"
"toml"
"ini"
"xml"
"php"
]);
description = "Config output format.\nOne of:\n`java json yaml toml ini xml php`.";
example = "json";
default = null;
};
formatter = mkOption {
type = types.anything;
description = "Serialisation/writer function to apply to `content`.\n`format` will auto-apply the correct format if the option value is valid.\nShould take `path: attrs:` and return a storepath.";
example = "pkgs.formats.yaml {}.generate";
default = null;
};
};
};
};
};
};
in
{
serviceDefs = mkOption {
type = types.attrsOf serviceConfig;
description = "Concrete service definitions, as per submodule options.\nPlease put service-related options into `services` instead, and use this to implement them.";
};
# lol https://github.com/NixOS/nixpkgs/issues/293510
_module.args = lib.mkOption {
internal = true;
};
# for internal use
_buildIdes = mkOption {
type = types.attrs;
internal = true;
};
};
#
# implementation
#
config =
let
branchOnConfig =
cfg:
{
text,
file,
content,
contentFmt,
}:
if (cfg.text != "") then
text
else if (cfg.file != null) then
file
else if (cfg.content != { }) then
if (cfg.format != null) then
content
else if (cfg.formatter != null) then
contentFmt
else
throw "`format` or `formatter` must be set for `content` ${cfg.content}!"
else
"";
in
{
# validate and complete the service configurations
_buildIdes.finalServices = builtins.mapAttrs (
name:
{
pkg,
args ? "",
exec ? "",
config,
}:
let
bin = if (exec == "") then pkgs.lib.getExe pkg else pkgs.lib.getExe' pkg exec;
ext =
if (config.ext != "") || (config.format != null) then "." + (config.ext or config.format) else "";
# we need this to create unit names that correspond to configs
cfgHash =
let
hashContent = builtins.hashString "sha256" (builtins.toJSON config.content);
in
branchOnConfig config {
text = builtins.hashString "sha256" config.text;
file = builtins.hashFile "sha256" config.file;
content = hashContent;
contentFmt = hashContent;
};
confFile =
let
writers = {
java = pkgs.formats.javaProperties { };
json = pkgs.formats.json { };
yaml = pkgs.formats.yaml { };
ini = pkgs.formats.ini { };
toml = pkgs.formats.toml { };
xml = pkgs.formats.xml { };
php = pkgs.formats.php { finalVariable = null; };
};
confPath = "config-${name}-${cfgHash}${ext}";
in
branchOnConfig config {
text = pkgs.writeText confPath config.text;
inherit (config) file;
content = writers.${config.format}.generate confPath config.content;
contentFmt = config.formatter confPath config.content;
};
finalArgs = builtins.replaceStrings [ "%CFG%" ] [ "${confFile}" ] args;
in
{
inherit name bin;
args = finalArgs;
unitName = "shell-${name}-${cfgHash}";
}
) config.serviceDefs;
# generate service scripts and create the shell
_buildIdes.shell =
let
mkWorks =
{
name,
unitName,
bin,
args,
}:
{
runner = ''
echo "[ides]: Starting ${name}.."
systemd-run --user -G -u ${unitName} ${bin} ${args}
'';
cleaner = ''
echo "[ides]: Stopping ${name}.."
systemctl --user stop ${unitName}
'';
};
works =
let
inherit (pkgs.lib) foldlAttrs;
in
foldlAttrs
(
acc: name: svc:
let
pair = mkWorks svc;
in
{
runners = acc.runners + pair.runner;
cleaners = acc.cleaners + pair.cleaner;
}
)
{
runners = "";
cleaners = "";
}
config._buildIdes.finalServices;
inherit (pkgs) writeShellScriptBin;
runners = writeShellScriptBin "ides" works.runners;
cleaners = writeShellScriptBin "et-tu" (
works.cleaners
+ ''
systemctl --user reset-failed
''
);
restart = writeShellScriptBin "restart" "et-tu; ides";
final =
let
shellArgs = config._buildIdes.shellArgs;
in
shellArgs
// {
nativeBuildInputs = (shellArgs.nativeBuildInputs or [ ]) ++ [
runners
cleaners
restart
];
shellHook =
(shellArgs.shellHook or "")
+ ''
ides
'';
};
in
config._buildIdes.shellFn final;
};
}

215
lib/build.nix Normal file
View File

@ -0,0 +1,215 @@
{
pkgs,
config,
...
}:
{
config =
let
# control flow monstrosity
branchOnConfig =
cfg:
{
text,
file,
content,
contentFmt,
}:
if (cfg.text != "") then
text
else if (cfg.file != null) then
file
else if (cfg.content != { }) then
if (cfg.format != null) then
content
else if (cfg.formatter != null) then
contentFmt
else
throw "`format` or `formatter` must be set for `content` value ${cfg.content}!"
else
"";
in
{
# validate and complete the service configurations
_buildIdes.finalServices = builtins.mapAttrs (
name:
{
pkg,
args ? "",
exec ? "",
config,
path,
socket,
timer,
}:
let
# make our best effort to use the correct binary
bin = if (exec == "") then pkgs.lib.getExe pkg else pkgs.lib.getExe' pkg exec;
# set file extension
ext =
if (config.ext != "") || (config.format != null) then "." + (config.ext or config.format) else "";
# config hash for unique service names
cfgHash =
let
# method to hash a set
hashContent = builtins.hashString "sha256" (builtins.toJSON config.content);
in
branchOnConfig config {
text = builtins.hashString "sha256" config.text;
file = builtins.hashFile "sha256" config.file;
content = hashContent;
contentFmt = hashContent;
};
confFile =
let
writers = {
java = pkgs.formats.javaProperties { };
json = pkgs.formats.json { };
yaml = pkgs.formats.yaml { };
ini = pkgs.formats.ini { };
toml = pkgs.formats.toml { };
xml = pkgs.formats.xml { };
php = pkgs.formats.php { finalVariable = null; };
};
# final config name
confPath = "config-${name}-${cfgHash}${ext}";
in
# write out config
branchOnConfig config {
text = pkgs.writeText confPath config.text;
inherit (config) file;
content = writers.${config.format}.generate confPath config.content;
contentFmt = config.formatter confPath config.content;
};
# template the config path into the launch command
cfgArgs = builtins.replaceStrings [ "%CFG%" ] [ "${confFile}" ] args;
# flatten unit options into cli args
sdArgs =
let
inherit (pkgs.lib) foldlAttrs;
inherit (builtins) concatStringsSep;
convertToArgList =
prefix: name: values:
(map (inner: "${prefix} ${name}=${inner}") values);
writeArgListFor =
attrs: prefix:
if (attrs != { }) then
concatStringsSep " " (
foldlAttrs (
acc: n: v:
acc + (convertToArgList prefix n v) + " "
) "" attrs
)
else
"";
in
concatStringsSep " " [
(writeArgListFor socket "--socket-property")
(writeArgListFor path "--path-property")
(writeArgListFor timer "--timer-property")
];
in
# transform into attrs that mkWorks expects to receive
{
inherit
bin
sdArgs
cfgArgs
;
unitName = "shell-${name}-${cfgHash}";
}
) config.serviceDefs;
# generate service scripts and create the shell
_buildIdes.shell =
let
# create commands to run and clean up services
mkWorks =
name:
{
unitName,
bin,
cfgArgs,
sdArgs,
}:
{
runner = pkgs.writeShellScriptBin "run" ''
echo "[ides]: starting ${name}.."
systemd-run --user -q -G -u ${unitName} ${sdArgs} ${bin} ${cfgArgs}
'';
cleaner = pkgs.writeShellScriptBin "clean" ''
echo "[ides]: stopping ${name}.."
systemctl --user -q stop ${unitName}
'';
status = pkgs.writeShellScriptBin "status" ''
systemctl --user -q status ${unitName}
'';
};
works = pkgs.lib.mapAttrs (
name: serviceConf: mkWorks name serviceConf
) config._buildIdes.finalServices;
# create the ides cli
cli = import ./cli.nix {
inherit (pkgs) writeShellScriptBin;
inherit (pkgs.lib) foldlAttrs;
inherit works;
};
# shell id is based on the services config
shellId = builtins.hashString "sha256" (builtins.toJSON config._buildIdes.finalServices);
monitor = import ./monitor.nix {
inherit (pkgs) writeShellScriptBin;
inherit shellId;
cli = pkgs.lib.getExe cli;
socat = pkgs.lib.getExe pkgs.socat;
# TODO make this timeout more lenient?
timeout = if (pkgs.lib.typeOf config.monitor == "int") then config.monitor else 20;
};
# create the ides shell
final =
let
inherit (config._buildIdes) shellArgs;
in
shellArgs
// {
nativeBuildInputs = (shellArgs.nativeBuildInputs or [ ]) ++ [
cli
];
shellHook =
let
autoRun =
if config.auto then
''
ides run
''
else
"";
monitorRun =
let
inherit (pkgs.lib) getExe;
in
if config.monitor then
''
systemd-run --user -q -G -u ides-${shellId}-monitor ${getExe monitor.daemon} $PWD
${getExe monitor.client} $$
''
else
"";
in
(shellArgs.shellHook or "")
+ ''
printf '[ides]: use "ides [action] [target]" to control services. type "ides help" to find out more.\n'
export IDES_CTL="/run/user/$(id -u)/ides-${shellId}.sock"
''
+ autoRun
+ monitorRun;
};
in
# TODO make this optionally return the shell components to allow composability with other dev shell solutions
config._buildIdes.shellFn final;
};
}

198
lib/cli.nix Normal file
View File

@ -0,0 +1,198 @@
{
foldlAttrs,
writeShellScriptBin,
works,
}:
let
statusFns = foldlAttrs (
acc: name: works:
acc
+ ''
function status-${name}() {
${works.status}/bin/status
}
''
) "" works;
statusAll =
''
function status-all() {
''
+ foldlAttrs (
acc: name: works:
acc + "${works.status}/bin/status\n"
) "" works
+ ''}'';
startAll =
''
function start-all() {
''
+ foldlAttrs (
acc: name: works:
acc + "${works.runner}/bin/run\n"
) "" works
+ ''}'';
startFns = foldlAttrs (
acc: name: works:
acc
+ ''
function start-${name}() {
${works.runner}/bin/run
}
''
) "" works;
stopAll =
''
function stop-all() {
''
+ foldlAttrs (
acc: name: works:
acc + "${works.cleaner}/bin/clean\n"
) "" works
+ ''
}
'';
stopFns = foldlAttrs (
acc: name: works:
acc
+ ''
function stop-${name}() {
${works.cleaner}/bin/clean
}
''
) "" works;
restartFns = foldlAttrs (
acc: name: _:
acc
+ ''
function restart-${name} {
stop-${name}
start-${name}
}
''
) "" works;
names = foldlAttrs (
acc: name: _:
acc ++ [ name ]
) [ ] works;
mkCmd = desc: fn: synonyms: {
inherit desc fn synonyms;
};
actions = [
(mkCmd "start service" "start" [
"run"
"r"
"up"
])
(mkCmd "stop service" "stop" [
"s"
"clean"
"et-tu"
"down"
])
(mkCmd "restart a service" "restart" [
"qq"
"re"
])
(mkCmd "show service status" "status" [
"stat"
"check"
"ch"
])
];
actionHelp = builtins.concatStringsSep "\n" (
map (cmd: ''
\t${cmd.fn}\t\tsynonyms: ${builtins.concatStringsSep " " cmd.synonyms}
\t- ${cmd.desc}
'') actions
);
help = ''
[ides]: use "ides [action] [target]" to control services.
actions:
${actionHelp}
\ttargets synonyms: t
\t- print a list of available targets
\thelp
\t- print this helpful information
target names are the same as the attribute name used to define a service.
eg. value of service.*.name, or serviceDefs.{name}
an empty target will execute the action on all available services.
'';
in
writeShellScriptBin "ides" ''
targets=(${builtins.concatStringsSep " " names})
function print-help() {
printf '${help}'
}
function list-targets() {
echo ''${targets[@]}
}
function check-target() {
found=1
for target in "''${targets[@]}"; do
if [ "$1" == "$target" ]; then
found=0
break
fi
done
printf $found
}
${statusFns}
${statusAll}
${startFns}
${startAll}
${stopFns}
${stopAll}
${restartFns}
function restart-all() {
stop-all
start-all
}
function action() {
action=$1
if [[ $# -gt 1 ]]; then
shift
for service in "$@"; do
if [[ $(check-target $service) -eq 0 ]]; then
$action-$service
else
echo "[ides]: no such target: $service"
fi
done
else
$action-all
fi
}
case $1 in
${builtins.concatStringsSep "\n" (
map (action: ''
${action.fn}|${builtins.concatStringsSep "|" action.synonyms})
shift
action ${action.fn} $@
;;
'') actions
)}
targets|t)
list-targets
;;
-h|h|help|*)
print-help
;;
esac
''

9
lib/ides.nix Normal file
View File

@ -0,0 +1,9 @@
{
...
}:
{
imports = [
./options.nix
./build.nix
];
}

125
lib/monitor.nix Normal file
View File

@ -0,0 +1,125 @@
{
socat,
writeShellScriptBin,
shellId,
cli,
timeout,
...
}:
let
wait = builtins.toString timeout;
in
{
daemon = writeShellScriptBin "ides-monitor-${shellId}" ''
BASE_PATH="$1"
SOCKET=/run/user/$(id -u)/ides-${shellId}.sock
# if socket exists, it's already being monitored
if [ -e "$SOCKET" ]; then
exit 1
fi
PIDS=()
# loop on socket forever
while true; do
# wait to receive a PID
PID=$(timeout ${wait} ${socat} UNIX-LISTEN:"$SOCKET" -)
# if received and unique, add to our watch
if [ "$?" -eq 0 ] && ! [[ "''${PIDS[@]}" =~ $PID ]]; then
echo adding $PID to watch
PIDS+=($PID)
echo pids are now ''${PIDS[@]}
fi
# check process statuses
DEAD=true
for INDEX in "''${!PIDS[@]}"; do
REMOVE=false
# get pid
CHECK="''${PIDS[$INDEX]}"
echo checking $CHECK
# check status
ALIVE=$(kill -0 "$CHECK" 2>&1 > /dev/null)
# determine eligibility to act as a service root
if $ALIVE; then
CMD=$(cat /proc/$CHECK/comm)
case "$CMD" in
# if host is an editor, we don't care about pwd
code|codium|zed|emacs|intellij*|sublime*)
DEAD=false
;;
# if it's a shell we really do
sh|bash|zsh|fish|nu|murex|*)
DIR=$(readlink /proc/$CHECK/cwd)
if [[ "$DIR" == "$BASE_PATH"* ]]; then
DEAD=false
else
REMOVE=true
fi
;;
esac
echo found $CHECK alive with $CMD in $DIR
else
echo found $CHECK dead
REMOVE=true
fi
if $REMOVE; then
echo removing ineligible pid $CHECK
unset "PIDS[$INDEX]"
fi
done
if "$DEAD"; then
echo no live pids, breaking
break
fi
done
# loop has broken, no valid pids left
echo stopping all!
${cli} stop
'';
# FIXME if the client finds its parent is an editor process,
# such as `code`, should it keep walking pids until the final parent?
client = writeShellScriptBin "ides-notify-${shellId}" ''
function get-parent() {
ps --no-header -o ppid:1 $1
}
function get-command() {
cat /proc/$1/comm
}
SOCKET=/var/run/user/$(id -u)/ides-${shellId}.sock
# wait for socket to come up
while ! [ -e "$SOCKET" ]; do
sleep 0.5
done
# check if our calling shell's parent process is direnv
PARENT=$(get-parent $1)
COMM=$(get-command $PARENT)
TARGET="$1"
echo found $COMM as parent
if [[ "$COMM" == "direnv" ]]; then
# if so, skip up another parent to get the process it is exporting to
TARGET=$(get-parent $PARENT)
fi
# tell monitor about the devshell
echo "$TARGET" | ${socat} - UNIX-CONNECT:"$SOCKET"
'';
}

146
lib/options.nix Normal file
View File

@ -0,0 +1,146 @@
{
pkgs,
...
}:
{
options =
let
inherit (pkgs) lib;
inherit (lib) types mkOption;
serviceConfig =
with types;
submodule {
options = {
pkg = mkOption {
type = package;
description = "Package to use for service.";
example = "pkgs.caddy";
};
exec = mkOption {
type = str;
description = "Alternative executable name to use from `pkg`.";
example = "caddy";
default = "";
};
args = mkOption {
type = str;
description = "Arguments to supply to the service binary. Writing %CFG% in this will template to your config location.";
example = "run -c %CFG% --adapter caddyfile";
default = "";
};
socket = mkOption {
type = attrsOf (listOf str);
description = "List of socket options for the unit (see `man systemd.socket`) - supplied as a list due to some options allowing duplicates.";
example = {
ListenStream = [ "/run/user/1000/myapp.sock" ];
};
default = { };
};
path = mkOption {
type = attrsOf (listOf str);
description = "List of path options for the unit (see `man systemd.path`) - supplied as a list due to some options allowing duplicates.";
example = {
PathModified = [ "/some/path" ];
};
default = { };
};
timer = mkOption {
type = attrsOf (listOf str);
description = "List of timer options for the unit (see `man systemd.path`) - supplied as a list due to some options allowing duplicates.";
example = {
OnActiveSec = [ 50 ];
};
default = { };
};
config = mkOption {
description = "Options for setting the service's configuration.";
default = { };
type = submodule {
options = {
text = mkOption {
type = str;
default = "";
description = "Plaintext configuration to use.";
example = ''
http://*:8080 {
respond "hello"
}
'';
};
ext = mkOption {
type = str;
default = "";
description = "If your service config requires a file extension, set it here. This overrides `format`'s output path'.";
example = "json";
};
file = mkOption {
type = nullOr path;
description = "Path to config file. This overrides all other values.";
example = "./configs/my-config.ini";
default = null;
};
content = mkOption {
type = nullOr attrs;
description = "Attributes that define your config values.";
default = null;
example = {
this = "that";
};
};
format = mkOption {
type = nullOr (enum [
"java"
"json"
"yaml"
"toml"
"ini"
"xml"
"php"
]);
description = "Config output format.\nOne of:\n`java json yaml toml ini xml php`.";
example = "json";
default = null;
};
formatter = mkOption {
type = types.anything;
description = "Serialisation/writer function to apply to `content`.\n`format` will auto-apply the correct format if the option value is valid.\nShould take `path: attrs:` and return a storepath.";
example = "pkgs.formats.yaml {}.generate";
default = null;
};
};
};
};
};
};
in
{
serviceDefs = mkOption {
type = types.attrsOf serviceConfig;
description = "Concrete service definitions, as per submodule options.\nPlease put service-related options into `options.services` instead, and use this to implement those options.";
};
auto = mkOption {
type = types.bool;
description = "Whether to autostart ides services at devshell instantiation.";
default = true;
};
monitor = mkOption {
type = types.either types.bool types.int;
description = "Enable, or set timeout period for, monitoring devshell activity and automatically destroying services after (experimental).";
default = true;
};
# to prevent generating docs for this option; see https://github.com/NixOS/nixpkgs/issues/293510
_module.args = mkOption {
internal = true;
};
# for internal use
_buildIdes = mkOption {
type = types.attrs;
internal = true;
};
};
}

View File

@ -64,55 +64,64 @@
description = "Additional config directives.";
default = "";
};
name = mkOption {
type = types.str;
description = "The name ides uses for this service.";
default = "redis";
};
};
config.serviceDefs.redis =
config.serviceDefs =
let
cfg = config.services.redis;
in
lib.mkIf cfg.enable {
pkg = pkgs.redis;
# make sure we get the server binary, not cli
exec = "redis-server";
args = "%CFG%";
config = {
ext = ".conf";
# these need to be made to match redis config
# variable names here
content = {
inherit (cfg) bind port databases;
unixsocket = cfg.socket;
unixsocketperm = cfg.socketPerms;
loglevel = cfg.logLevel;
# use a customisable name in case the user needs several instances
"${cfg.name}" = {
pkg = pkgs.redis;
# make sure we get the server binary, not cli
exec = "redis-server";
args = "%CFG%";
config = {
ext = ".conf";
# these need to be made to match redis config
# variable names here
content = {
inherit (cfg) bind port databases;
unixsocket = cfg.socket;
unixsocketperm = cfg.socketPerms;
loglevel = cfg.logLevel;
};
# a formatter needs to take in a set of
# attrs and write out a file
formatter =
let
# set up serialisation for all types
serialise = {
int = builtins.toString;
bool = b: if b then "yes" else "no";
string = s: s;
path = builtins.toString;
null = _: _;
list = builtins.concatStringsSep " ";
float = builtins.toString;
set = throw "cannot serialise a set in redis format";
lambda = throw "cannot serialise a lambda, wtf?";
};
in
# create a lambda that can serialise to redis config
path: attrs:
let
text =
(lib.foldlAttrs (
acc: n: v:
if (v != null) then acc + "${n} ${serialise.${builtins.typeOf v} v}" + "\n" else acc
) "" attrs)
+ cfg.extraConfig;
in
(pkgs.writeText path text).outPath;
};
# a formatter needs to take in a set of
# attrs and write out a file
formatter =
let
# set up serialisation for all types
serialise = {
int = builtins.toString;
bool = b: if b then "yes" else "no";
string = s: s;
path = builtins.toString;
null = _: _;
list = builtins.concatStringsSep " ";
float = builtins.toString;
set = throw "cannot serialise a set in redis format";
lambda = throw "cannot serialise a lambda, wtf?";
};
in
# create a lambda that can serialise to redis config
path: attrs:
let
text =
(lib.foldlAttrs (
acc: n: v:
if (v != null) then acc + "${n} ${serialise.${builtins.typeOf v} v}" + "\n" else acc
) "" attrs)
+ cfg.extraConfig;
in
(pkgs.writeText path text).outPath;
};
};
}