Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Its probably that I grew up with c like languages. When I try to read something like

{ pkgs }: { deps = [ pkgs.cowsay ]; }

It’s meaningless to my brain.



To put it in a C context: it’s a function with a single argument ‘pkgs’, which returns a struct with one field ‘deps’, which is an array of package structs.


So why did they chose such unconventional syntax? Just for the hell of it? There doesn't seem to be any advantage and it definitely puts a lot of people off.


The syntax is not so unconventional compared to, say, modern javascript:

({ pkgs }) => { deps: [ pkgs.cowsay ] }

or python:

lambda pkgs: { deps: [pkgs.cowsay] }

I guess really the question is why create a new language rather than using an existing one? The language needed to be functional, lazy, and good at managing packages, and not much else. I guess they felt that no existing language fits the bill (Haskell does I guess but it's got a lot of baggage). I can't claim to know much about its history.


Translating to modern JavaScript in three easy steps:

  { pkgs }: { deps = [ pkgs.cowsay ]; }
  ({ pkgs }): { deps = [ pkgs.cowsay ]; }
  ({ pkgs }) => { deps = [ pkgs.cowsay ]; }
  ({ pkgs }) => ({ deps = [ pkgs.cowsay ]; })


Your translation is incomplete. You're ending with:

> ({ pkgs }) => ({ deps = [ pkgs.cowsay ]; })

But the syntax for declaring objects in javascript differs. It would be:

    ({ pkgs }) => ({ deps: pkgs.cowsay })
The fact that '{ x = 1; }' is declaring an object, identical to '{x: 1}' in javascript, is important.

It's also worth pointing out that, while '{ pkgs }:' is idiomatic nix, idiomatically in javascript you'd probably just take multiple arguments (so '(pkgs) =>', not '({ pkgs }) =>' in js). The reason nix uses an object there is because a nix function only takes one argument, so there's a fairly common convention to make that argument an attr in order to simulate multiple arguments. That convention doesn't exist in quite the same way in js.


You forgot the array ({ pkgs }) => ({ deps: [pkgs.cowsay] })

I also translate all nix to JavaScript in my brain


Helpful. There once was a command line tool that would turn a C declaration into english (like cdecl.org) I’d love to see something like that for the nix language (and kotlin too since I’m dreaming)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: