svanderburg / nijs

NiJS: An internal DSL for Nix in JavaScript
MIT License
56 stars 4 forks source link

Purpose #1

Open CMCDragonkai opened 10 years ago

CMCDragonkai commented 10 years ago

Just wondering if the purpose of this project is to provide a way for javascript to interface/RPC to Nix?

What are you using it for at a high level?

Replace npm?

svanderburg commented 10 years ago

That's one of its objectives. To integrate a JavaScript program with Nix, you typically have to generate Nix expressions and call Nix, which is ugly. As an alternative, you can also provide an abstraction that let's someone call a JavaScript function that implements a Nix build. Actually, I'm using this for a private project implemented in JavaScript that utilizes Nix to perform deployment operations.

However, there are more language integration use cases that NiJS implements, such as defining Nix packages in JavaScript, implementing build procedures in JavaScript and so on. Furthermore, NiJS is also a simple stand alone package manager.

On Mon, Jul 28, 2014 at 8:46 AM, Roger Qiu notifications@github.com wrote:

Just wondering if the purpose of this project is to provide a way for javascript to interface/RPC to Nix?

What are you using it for?

— Reply to this email directly or view it on GitHub https://github.com/svanderburg/nijs/issues/1.

CMCDragonkai commented 10 years ago

such as defining Nix packages in JavaScript

I haven't seen any packages inside nixpkgs that are defined with javascript. But I have seen the https://github.com/NixOS/nixpkgs/blob/master/pkgs/top-level/node-packages.json

implementing build procedures in JavaScript and so on

You mean replacing something like grunt or gulp?

Furthermore, NiJS is also a simple stand alone package manager.

What kind of packages? All the packages that are in nixpkgs?

Does this project have any relationship with npm2nix or npm in general?

Could one replace the configuration.nix file with something written in javascript using nijs?

svanderburg commented 10 years ago

I haven't seen any packages inside nixpkgs that are defined with javascript. But I have seen the https://github.com/NixOS/nixpkgs/blob/master/pkgs/top-level/node-packages.json

Everything in the Nixpkgs repository is implemented in Nix's own DSL -- the Nix expression language. However, if you're looking for package build recipes implemented JavaScript, have a look inside the tests/pkgs/ and tests/pkgs-async/ folders of this package. These contain a small set of packages defined with JavaScript.

The file you're referring to is a set of npm2nix generated Nix expressions (defined in the Nix language) that build Node.js packages.

You mean replacing something like grunt or gulp?

No. What Nix basically does is setting up environments in which builds can be performed and their dependencies can be found. You typically execute shell commands in such an environment, such as: ./configure; make; make install, but basically you can invoke any build tool you like, such as Perl's MakeMaker, Apache Ant, SCons, CMake etc.

What NiJS also offers is instead of executing bash shell commands, you can execute JavaScript "commands" in such an enviroment to e.g. perform a build. So it could also replace bash as a scripting language.

What kind of packages? All the packages that are in nixpkgs?

No. The packages that reside in the tests/pkgs-async/ folder of this package can also be deployed standalone.

However, standalone deployment is just a simple proof of concept experiment. Not an intended replacement for Nix/NPM.

Does this project have any relationship with npm2nix or npm in general?

It has no direct relationship with npm2nix, although it may utilize packages in Nixpkgs that are generated by it.

The only relationship that NiJS has with npm is that NiJS is also an NPM package.

Could one replace the configuration.nix file with something written in javascript using nijs?

Well it is not entirely "replacing" it. You can define the same config in JavaScript and let NiJS generate a Nix expression from it. This resulting expression could be used by nixos-rebuild to construct a system configuration.

So what you can do is express the same config in JavaScript. However, it does not replace it, since this file is still being generated.

Same thing holds btw. for other Nix applications.

CMCDragonkai commented 10 years ago

Hmm I can see how using javascript to generate nix expressions can provide greater flexibility. You get all the access to javascript libraries and node's functionalities while still being able to access Nix's functions.

So in a nutshell, NiJS is a transpiler, that generates Nix expressions, but it also kind of embeds Nix as a embedded DSL inside Javascript, thus allowing one to mix javascript and nix together.

I'm curious, what's your private project? You wrote you were using javascript with nix to deploy things. What was lacking in Nix that you needed Javascript to wrap around it? Was it just for the convenience of integrating with the rest of your javascript written application?

svanderburg commented 10 years ago

The private project is actually a customization and sort-of product-line tool implemented in JavaScript allowing someone to configure the app's components and behavior. Some of the options also affect the way packages are built.

What I could do is generate Nix expressions through strings. But this has all kinds of inconveniences: you have to escape certain characters, take invalid identifiers into account etc. etc.

As a solution I have decided to implement an internal DSL to make code generation more convenient.

wmertens commented 6 years ago

I ran into https://github.com/NixOS/nix/issues/2232 and I wondered how well NodeJS would fare at implementing a Nix interpreter - it seems that this is it?

I saw that it calls out to nix-build to build derivations, and that of course uses the nix code to evaluate the expression. I wonder how far NiJS would be from replacing nix-build?

And of course, it would be great to build a language extension for vscode with NiJS - especially something like prettier.

trusktr commented 4 years ago

it would be great to build a language extension for vscode with NiJS

This would be possible by simply converting NiJS source code to TypeScript, which already has all the language support in VS Code, Prettier, ES Lint, etc.

wmertens commented 4 years ago

@trusktr no - I meant, use NiJS to interpret Nix code and provide advanced editing functions like scoped names etc (that might be a disaster due to recursive eval ;) ). I did not mean being able to edit NiJS code, it's already javascript.

fogti commented 2 years ago

@wmertens I don't think that would be possible with NiJS, but you could take a look at https://github.com/YZITE/nix2js, which tries to do that somewhat, and might be integrated with e.g. NiJS as a backend/handler runtime, to probably achieve that. (recursive lazy eval is possible in JS, although non-trivial to get right, currently I just resort to make all attrset elements lazy, and evaluate the rest instantly (e.g. everything outside, and the stuff directly inside of the rhs of a value "assignment", as soon as it gets accessed)).

milahu commented 2 years ago

@wmertens

I ran into NixOS/nix#2232 and I wondered how well NodeJS would fare at implementing a Nix interpreter - it seems that this is it?

see my nix-eval-js for a nix interpreter in javascript

goal: prototype an incremental nix evaluator for rnix-lsp intellisense

wmertens commented 2 years ago

@milahu cool! What's the state of your project?

milahu commented 2 years ago

1% : D