Help building a go package

I’d like to use dstask in NixOS so I submitted a pull request to add it, but I was asked to build from the source code rather than use the binary. That’s a bit more complicated to do so I haven’t figured out how yet. This is my current attempt:

{ stdenv, buildGoModule, fetchFromGitHub }:

buildGoModule rec {
  pname = "dstask";
  version = "0.17";

  src = fetchFromGitHub {
    owner = "naggie";
    repo = "dstask";
    rev = "${version}";
    sha256 = "1z00f7ar8a0ahspgc240iq7cka1anls383d1fv3i17sb6ajmp6rc";
  };

  modSha256 = "1ydzqg8p2d514sdb34b2p6k1474nr1drrn3gay2cpyhrj5l51cj3";

  subPackages = [ "." ];

  meta = with stdenv.lib; {
    description = "Command line todo list with super-reliable git sync";
    homepage = "https://github.com/naggie/dstask";
    license = licenses.mit;
    maintainers = with maintainers; [ stianlagstad ];
    platforms = platforms.linux;
  };
}

This is what happens when I build:

> nix-build -A dstask -K                                         
these derivations will be built:
  /nix/store/kjg11z4j8cspzg03jg52bdxk0nxhghr9-dstask-0.17.drv
building '/nix/store/kjg11z4j8cspzg03jg52bdxk0nxhghr9-dstask-0.17.drv'...
unpacking sources
unpacking source archive /nix/store/9kxb3dig60vxwf52gdz498kbh7b5w6n8-source
source root is source
patching sources
configuring
building
Building subPackage ./.
go: downloading golang.org/x/sys v0.0.0-20181213200352-4d1cda033e06
go: downloading gopkg.in/yaml.v2 v2.2.2
go: downloading github.com/gofrs/uuid v3.2.0+incompatible
runtime/cgo
net
github.com/gofrs/uuid
golang.org/x/sys/unix
gopkg.in/yaml.v2
os/user
github.com/naggie/dstask
installing
post-installation fixup
find: '/nix/store/599xncfk2ayd40v2v3ns851nv1jrv27c-dstask-0.17/bin': No such file or directory
shrinking RPATHs of ELF executables and libraries in /nix/store/599xncfk2ayd40v2v3ns851nv1jrv27c-dstask-0.17
strip is /nix/store/3b3ighb83nhifa1v4n7855hlbdl1mhf9-binutils-2.31.1/bin/strip
patching script interpreter paths in /nix/store/599xncfk2ayd40v2v3ns851nv1jrv27c-dstask-0.17
checking for references to /build/ in /nix/store/599xncfk2ayd40v2v3ns851nv1jrv27c-dstask-0.17...
/nix/store/599xncfk2ayd40v2v3ns851nv1jrv27c-dstask-0.17
> ls -ahl /nix/store/599xncfk2ayd40v2v3ns851nv1jrv27c-dstask-0.17
total 980K
dr-xr-xr-x    2 root root   4.0K Jan  1  1970 .
drwxrwxr-t 2190 root nixbld 972K May  9 19:55 ..

Note the find: '/nix/store/599xncfk2ayd40v2v3ns851nv1jrv27c-dstask-0.17/bin': No such file or directory there. Does that mean that the build of https://github.com/naggie/dstask/ outputs the binary in another folder or something? Thanks for reading.

The main package is in the cmd submodule and not on the top level. I have no clue how to build such a go module. But perhaps adding cmd to submodules will already work?

Yes you just need define subPackages = [ "cmd" ];

Excerpts from Norbert Melzer via NixOS Discourse’s message of May 10, 2020 1:00 pm:

Thank you both! That got me further.

Current attempt:

{ stdenv, buildGoModule, fetchFromGitHub }:

buildGoModule rec {
  pname = "dstask";
  version = "0.17";

  src = fetchFromGitHub {
    owner = "naggie";
    repo = "dstask";
    rev = "${version}";
    sha256 = "1z00f7ar8a0ahspgc240iq7cka1anls383d1fv3i17sb6ajmp6rc";
  };

  modSha256 = "1ydzqg8p2d514sdb34b2p6k1474nr1drrn3gay2cpyhrj5l51cj3";

  subPackages = [ "cmd" ];

  meta = with stdenv.lib; {
    description = "Command line todo list with super-reliable git sync";
    homepage = "https://github.com/naggie/dstask";
    license = licenses.mit;
    maintainers = with maintainers; [ stianlagstad ];
    platforms = platforms.linux;
  };
}

Building:

> nix-build -A dstask -K                                               
these derivations will be built:
  /nix/store/hl4yhf02yy8fn7r6iigmc2dcbaga0wqw-dstask-0.17.drv
building '/nix/store/hl4yhf02yy8fn7r6iigmc2dcbaga0wqw-dstask-0.17.drv'...
unpacking sources
unpacking source archive /nix/store/9kxb3dig60vxwf52gdz498kbh7b5w6n8-source
source root is source
patching sources
configuring
building
Building subPackage ./cmd
go: downloading github.com/mvdan/xurls v1.1.0
go: downloading gopkg.in/yaml.v2 v2.2.2
go: downloading github.com/gofrs/uuid v3.2.0+incompatible
go: downloading golang.org/x/sys v0.0.0-20181213200352-4d1cda033e06
github.com/mvdan/xurls
runtime/cgo
net
github.com/gofrs/uuid
golang.org/x/sys/unix
gopkg.in/yaml.v2
os/user
github.com/naggie/dstask
github.com/naggie/dstask/cmd
installing
post-installation fixup
shrinking RPATHs of ELF executables and libraries in /nix/store/6kza9hx3rhcnbqialkmwbksg4khkmp2f-dstask-0.17
shrinking /nix/store/6kza9hx3rhcnbqialkmwbksg4khkmp2f-dstask-0.17/bin/cmd
strip is /nix/store/3b3ighb83nhifa1v4n7855hlbdl1mhf9-binutils-2.31.1/bin/strip
stripping (with command strip and flags -S) in /nix/store/6kza9hx3rhcnbqialkmwbksg4khkmp2f-dstask-0.17/bin
patching script interpreter paths in /nix/store/6kza9hx3rhcnbqialkmwbksg4khkmp2f-dstask-0.17
checking for references to /build/ in /nix/store/6kza9hx3rhcnbqialkmwbksg4khkmp2f-dstask-0.17...
/nix/store/6kza9hx3rhcnbqialkmwbksg4khkmp2f-dstask-0.17

This got me the binary /nix/store/6kza9hx3rhcnbqialkmwbksg4khkmp2f-dstask-0.17/bin/cmd. That binary seems to be dstask:

> /nix/store/6kza9hx3rhcnbqialkmwbksg4khkmp2f-dstask-0.17/bin/cmd 
Could not find git repository at /home/stian/.dstask, please clone or create. Try `dstask help` for more information.

Trying to enter a nix shell with dstask by doing nix-shell -A dstask --pure (I followed this guide to begin with) gives me a shell where neither a cmd or dstask program is available:

> nix-shell -A dstask --pure

[nix-shell:~/devp/nixpkgs]$ dstask
dstask: command not found

[nix-shell:~/devp/nixpkgs]$ cmd
cmd: command not found

[nix-shell:~/devp/nixpkgs]$ echo $PATH
/nix/store/w0fp3sgvgs59cvbb14lsa1qmsgy9gbbp-bash-interactive-4.4-p23/bin:/nix/store/g7ncfj08vhkmxk40hk29lqm6qyax1inb-remove-references-to/bin:/nix/store/dn45hrr746zksa4yi0sqaw5vlvp3h84q-go-1.14.2/bin:/nix/store/sicn6b10ixbs6435d7dr630i2hxphfpr-patchelf-0.9/bin:/nix/store/k930l9lckxjf4zmafv4aapvdbd2sibmj-gcc-wrapper-9.3.0/bin:/nix/store/52x4908vr922dhnxz1i5rfnrsq244vzc-gcc-9.3.0/bin:/nix/store/g5lavnivfasac7m15g3mh9lhmhsr5vl4-glibc-2.30-bin/bin:/nix/store/ca9mkrf8sa8md8pv61jslhcnfk9mmg4p-coreutils-8.31/bin:/nix/store/3gd7zz0s80k50amwzj3015ddz0sd9inl-binutils-wrapper-2.31.1/bin:/nix/store/3b3ighb83nhifa1v4n7855hlbdl1mhf9-binutils-2.31.1/bin:/nix/store/g5lavnivfasac7m15g3mh9lhmhsr5vl4-glibc-2.30-bin/bin:/nix/store/ca9mkrf8sa8md8pv61jslhcnfk9mmg4p-coreutils-8.31/bin:/nix/store/ca9mkrf8sa8md8pv61jslhcnfk9mmg4p-coreutils-8.31/bin:/nix/store/hg3albf7g05ljfqrfjhd58rblimrp6ph-findutils-4.7.0/bin:/nix/store/k7rakfl1ld9ycfm1zzszly0161h6jlcp-diffutils-3.7/bin:/nix/store/jpqlmf3wqg281j8fdz50kjl525pfsxjc-gnused-4.8/bin:/nix/store/8pajzfyqx1v7dz1znrnrc4pqj5rmnx24-gnugrep-3.4/bin:/nix/store/sya6knvydv0am22x477p9kpg187882vq-gawk-5.1.0/bin:/nix/store/0s93dpvxn7vaaah3n1lgz0q0xqdfsbkj-gnutar-1.32/bin:/nix/store/6yqb5dfmlsx0zr0q8xzi8v4wbrzmvwpf-gzip-1.10/bin:/nix/store/hgp00a1rgwqxjcrkqhbjrfb5df7wjhl5-bzip2-1.0.6.0.1-bin/bin:/nix/store/998ykzc5mazwak08hxayqmyqkyz18fcb-gnumake-4.2.1/bin:/nix/store/kgp3vq8l9yb8mzghbw83kyr3f26yqvsz-bash-4.4-p23/bin:/nix/store/c8nzpy2f2sz0w3mp4x8gkpn4hd4l2420-patch-2.7.6/bin:/nix/store/6ca524vb3yg59ag3khm0010591rny9v1-xz-5.2.5-bin/bin

Do you have any tips to guide me towards getting an executable named dstask?

nix-shell -A foo gives a shell suitable to build foo, not to use foo.
I think you meant: nix-shell -I nixpkgs=. -p dstask

Thanks symphorien! That gives me a shell where cmd is available as dstask :slightly_smiling_face:

I wanted to build this tool for NixOS as well. Here is my proposal :slight_smile:

1 Like