Files
skillshare/internal/version/nix.go
T
Charitarth ChughandClaude Opus 5.5 64c8a3f34e feat: add Nix flake and handle Nix-store installs in upgrade (#764)
* feat: add Nix flake and stop self-upgrade of Nix-store installs

Add flake.nix with packages, overlays.default, checks and a formatter,
building nix/package.nix with the unit tests in the Nix sandbox. The
version comes from the Release Please manifest, so releases need no Nix
edit. A Nix workflow runs `nix flake check` on non-docs changes.

`skillshare upgrade` now detects binaries under /nix/store/ and prints
`nix profile upgrade skillshare` instead of replacing a read-only store
path; update notices and doctor suggest the same command.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(upgrade): give Nix installs the right update path everywhere

The first flake commit sent every /nix/store binary to `nix profile
upgrade`, failed a plain `skillshare upgrade` with exit 1 even when the
CLI was current, and left the dashboard with a generic 500.

- Tell `nix profile` installs (the profile manifest lists the store path)
  from NixOS, Home Manager and overlay installs, which are told to update
  their configuration and rebuild. Detection lives in one helper shared by
  the update notice, doctor, upgrade and the dashboard.
- `upgrade` on Nix reports "already up to date" or prints the hint and
  exits 0; the built-in skill still updates.
- The dashboard runs the upgrade so the skill updates, then answers
  409 `upgrade.nix` with the command when a newer CLI waits on Nix. The
  UI shows localized text for it in all 11 locales.
- Installs skip the unit tests; the flake's checks enable them. postPatch
  lowers only the go directive's patch version and fails evaluation with
  a clear message when go.mod needs a newer Go minor. CI also builds the
  package on macOS.
- Docs, translations, the built-in skill reference and the release topic
  describe the new behaviour.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 15:53:57 +08:00

65 lines
2.0 KiB
Go

package version
import (
"encoding/json"
"os"
"path/filepath"
"slices"
"strings"
)
// nixProfileElement is the name `nix profile add github:runkids/skillshare`
// gives the flake's default package, and the one the upgrade command names.
const nixProfileElement = "skillshare"
// nixProfileManifests returns the manifests of the user's `nix profile`
// profiles: ~/.nix-profile, and its XDG location when use-xdg-base-directories
// is set.
func nixProfileManifests() []string {
var paths []string
if home, err := os.UserHomeDir(); err == nil {
paths = append(paths, filepath.Join(home, ".nix-profile", "manifest.json"))
state := os.Getenv("XDG_STATE_HOME")
if state == "" {
state = filepath.Join(home, ".local", "state")
}
paths = append(paths, filepath.Join(state, "nix", "profile", "manifest.json"))
}
return paths
}
// nixProfileManifest mirrors the subset of a version 3 `nix profile` manifest
// we need. nix-env profiles have no manifest.json, and older versions list
// elements without names, so neither parses into this.
type nixProfileManifest struct {
Elements map[string]struct {
StorePaths []string `json:"storePaths"`
} `json:"elements"`
}
// detectNixMethod tells a `nix profile` install, which one command upgrades,
// from a binary a NixOS, Home Manager or nix-env setup provides. Home Manager
// adds one combined package to the profile, so the skillshare store path is
// only an element's own when the user added skillshare directly.
func detectNixMethod(execPath string, manifests []string) InstallMethod {
parts := strings.SplitN(execPath, "/", 5) // "", "nix", "store", "<hash>-<name>", rest
if len(parts) < 4 {
return InstallNix
}
storePath := "/nix/store/" + parts[3]
for _, manifest := range manifests {
data, err := os.ReadFile(manifest)
if err != nil {
continue
}
var m nixProfileManifest
if json.Unmarshal(data, &m) != nil {
continue
}
if slices.Contains(m.Elements[nixProfileElement].StorePaths, storePath) {
return InstallNixProfile
}
}
return InstallNix
}