I’m making my very first NixOS package as a beginner user. It’s an lsp server to work on a static site generator called Hugo but I’m clueless about what’s going wrong with it in spite of checking similar topics on the forum as well as going through the official guide step by step a couple of times.
So, basically I get the following error:
error: attribute ‘nix’ in selection path ‘go-template-lsp.nix’ not found
I feel there isn’t wrong with the code but maybe the way I setup Nix has to do with this since it is managed by the following flake:
{ stdenv, fetchFromGithub, go }:
stdenv.mkDerivation {
pname = “go-template-lsp”;
version = “0.3.4”;
src = fetchFromGithub {
owner = “yayolande”;
repo = “go-template-lsp”;
rev = “v0.3.4”;
sha256 = “nix-prefetch-url command output”;
};
buildInputs = [ go ];
preInstall =
‘’
go get -u
go build
‘’;
installPhase =
‘’
runhook preInstall
mkdir -p $out/bin
cp go-template-lsp $out/bin
‘’;
}
What’s selection path? How can I check it with a nix command? What am I missing/doing wrong this time?
I’d love to learn why things work/don’t work so if you feel I should research some concepts please make your suggestions. I’d be happy to check them out
Without the full error message it is somewhat hard to tell. But at least this line contains a syntax error (overlays = ; is missing a value), if both arguments are empty you can also elid them or, in case you want to define them explicitly, replace overlays = ; with overlays = [];. The syntax error would throw quite a different error message, so maybe this was just a manual transfer issue when posting to discourse.
The go-template-lsp.nix itself does not look like it contains any obvious errors. However, it will almost certainly not build with the nix sandbox as go get -u will require an internet connection. Please refer to the nixpkgs manual on packaging Go applications for the relevant packaging helpers. You can also find various examples in nixpkgs, like wego.
Based on the error message, I suspect you forgot the ./ on what you intended to be a path-type. But the file giving the error is very likely not among those you pasted.
Here you go a new version with -v flag passed to nix-build command:
~/waves-nixconfig/packages (main *%)-> nix-build -Av go-template-lsp.nix
error: cannot evaluate a function that has an argument without a value ('stdenv')
Nix attempted to evaluate a function as a top level expression; in
this case it must have its arguments supplied either by default
values, or passed explicitly with '--arg' or '--argstr'. See
https://nix.dev/manual/nix/stable/language/syntax.html#functions.
at /home/wavesinaroom/waves-nixconfig/packages/go-template-lsp_old.nix:1:3:
1| { stdenv, fetchFromGithub, go }:
| ^
2| stdenv.mkDerivation {
Sorry about not having the overlay square brackets in the question. I wasn’t easy to format my code in my question. It does exist in my source code though Thanks for pointing that out anyway!
First of all, thanks for taking time to read my question. Again this is my first time packaging on NixOS therefore I didn’t know about the existence of buildGoModule till now, so cheers for that.
Now, I spent a bit of time reading the documentation and coding a new expression with buildGoModule. I made more sense to do it it that way because I don’t need to bring go into the game like I did on my first version with buildInputs = [ go];. However I’ve got a similar output to the one I shared with @NobbZ.
Here you go the error message:
~/waves-nixconfig/packages (main *%)-> nix-build -Av go-template-lsp.nix
error: cannot evaluate a function that has an argument without a value ('buildGoModule')
Nix attempted to evaluate a function as a top level expression; in
this case it must have its arguments supplied either by default
values, or passed explicitly with '--arg' or '--argstr'. See
https://nix.dev/manual/nix/stable/language/syntax.html#functions.
at /home/wavesinaroom/waves-nixconfig/packages/go-template-lsp.nix:1:3:
1| { buildGoModule, fetchFromGithub }: {
| ^
2| lsp = buildGoModule (finalAttrs: {
This is my code:
{ buildGoModule, fetchFromGithub }: {
lsp = buildGoModule (finalAttrs: {
pname = "go-template-lsp.nix";
version = "0.3.4";
src = fetchFromGithub {
owner = "yayolande";
repo = "go-template-lsp";
version = "v${finalAttrs.version}";
hash = "sha256-18jcyknq9yhbcilk0gbshvbiszyffbjhxn2g6gcrrmnbykns7gj4";
};
meta = {
description =
"Language server to be used with Hugo static page generator";
homepage = "https://github.com/yayolande/go-template-lsp";
};
});
}
What I don’t this time is that Nix complains about not being able to know what buildGoModule is but I find that weird because there’s already a default.nix file that helps Nix use the pkgs. If this is not the case, what’s causing the problem?
No! In impure evaluation (as it happens here) there is a difference between import nixpkgs {} and import nixpkgs {config = {}; overlays = [];}. The former will lookup ~/.config/nixpkgs/config.nix for config as well as ~/.config/nixpkgs/overlays.nix for overlays.
I took the OP packages/default.nix and fixed the obvious syntax errors, then I tried to build with the go-template-lsp.nix from the replied to post.
I again fixed issues as I went, never seeing the error you mentioned.
What I did though was using nix-build -A go-template-lsp.lsp, as that is the attribute path you have choosen for whatever reason.
I was able to get to the point that the deps should be fetched, though that fails, as the repo in question is broken, as it doesn’t commit a go.sum (lock file for go modules), this is required.
This was helpful, my derivation went well, thanks! I made changes on my side and set vendorHash to null meaning vendorHash = null so this reply complements your changes.
Just last two questions:
I couldn’t find anywhere in my code a line that chooses that attribute path. How and where did I choose? Sorry this is a bit confusing
Last question: How did you copy and paste the git diff like of your changes and my code?
default.nix specifies, go-template-lsp and withing that you have nested the actual derivation under the lsp attribute for whatever reason. It is more common to have not wrap within a callpackaged file.
I selected it in the terminal and then copied it, pasting it back into the browser. Just standard copy/paste workflow.
vendorHash = null is the same as vendorHash = "", I do not see any reason why it should succeed using that, as upstream still lacks a go.sum.
Yeah, you’re right! I see that now, the reason for doing that is because I followed the official guide to strictly to be sure that the derivation work but indeed there’s no need for doing that I get what you mean.
Alright, I thought the forum had a special option for that
I didn’t get any error here but I’ll double-check that. The original repo had an incomplete checklist of features so it’s not surprise that it’s broken, you found that out. That’s a bummer, but I’m cool with having the experience of trying to make my first nix package.