Skip to content

Commit 59a3990

Browse files
committed
fix: nix build for sbom-merger
Reorganize codebase into standard Go project structure with cmd/ directories to build two separate binaries and shared types in a common package.
1 parent 99c04f9 commit 59a3990

File tree

6 files changed

+177
-140
lines changed

6 files changed

+177
-140
lines changed

README.md

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ A comprehensive SBOM (Software Bill of Materials) generator for systems running
2222
Clone the repository and enter the development shell:
2323

2424
```bash
25-
git clone <repository-url>
25+
git clone https://github.com/supabase/ubuntu-nix-sbom
2626
cd ubuntu-nix-sbom
2727
nix develop
2828
```
@@ -283,24 +283,54 @@ Both the PR check and release workflows automatically validate SPDX output:
283283

284284
## Development
285285

286+
### Project Structure
287+
288+
The project follows the standard Go project layout:
289+
290+
```
291+
.
292+
├── internal/
293+
│ └── sbom/
294+
│ └── types.go # Shared types and utilities (package sbom)
295+
├── cmd/
296+
│ ├── ubuntu-sbom/
297+
│ │ └── main.go # Ubuntu SBOM generator binary
298+
│ └── sbom-merge/
299+
│ └── main.go # SBOM merger binary
300+
├── flake.nix # Nix flake configuration
301+
└── go.mod # Go module (github.com/supabase/ubuntu-nix-sbom)
302+
```
303+
304+
The `internal/` directory is a Go convention that prevents external packages from importing the code, ensuring the shared types remain internal to this project.
305+
306+
### Development Environment
307+
286308
Enter the development shell:
287309

288310
```bash
289311
nix develop
290312
```
291313

292314
This provides:
293-
- Go toolchain
315+
- Go toolchain (with Go modules support)
294316
- gopls (Go language server)
295317
- sbomnix
296318
- Formatting tools (nixfmt, shellcheck, shfmt, gofmt)
297319
- Pre-commit hooks (automatically installed)
298320

321+
### Building Binaries
322+
299323
Build the Go binaries manually:
300324

301325
```bash
302-
go build -o ubuntu-sbom main.go
303-
go build -o sbom-merge merge.go
326+
# Build Ubuntu SBOM generator
327+
go build -o ubuntu-sbom ./cmd/ubuntu-sbom
328+
329+
# Build SBOM merger
330+
go build -o sbom-merge ./cmd/sbom-merge
331+
332+
# Build both
333+
go build ./cmd/...
304334
```
305335

306336
### Code Formatting

merge.go renamed to cmd/sbom-merge/main.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ import (
99
"regexp"
1010
"strings"
1111
"time"
12+
13+
"github.com/supabase/ubuntu-nix-sbom/internal/sbom"
1214
)
1315

14-
func mainMerge() {
16+
func main() {
1517
var (
1618
ubuntuSBOM = flag.String("ubuntu", "", "Path to Ubuntu SBOM JSON file")
1719
nixSBOM = flag.String("nix", "", "Path to Nix SBOM JSON file")
@@ -38,7 +40,7 @@ func mainMerge() {
3840

3941
type SBOMMerger struct{}
4042

41-
func (m *SBOMMerger) Merge(ubuntuPath, nixPath string) (*SPDXDocument, error) {
43+
func (m *SBOMMerger) Merge(ubuntuPath, nixPath string) (*sbom.SPDXDocument, error) {
4244
// Load Ubuntu SBOM
4345
ubuntuDoc, err := m.loadDocument(ubuntuPath)
4446
if err != nil {
@@ -52,23 +54,23 @@ func (m *SBOMMerger) Merge(ubuntuPath, nixPath string) (*SPDXDocument, error) {
5254
}
5355

5456
// Create merged document
55-
mergedDoc := &SPDXDocument{
57+
mergedDoc := &sbom.SPDXDocument{
5658
SPDXVersion: "SPDX-2.3",
5759
DataLicense: "CC0-1.0",
5860
SPDXID: "SPDXRef-DOCUMENT",
5961
Name: fmt.Sprintf("Ubuntu-Nix-System-SBOM-%s", time.Now().Format("2006-01-02")),
60-
DocumentNamespace: fmt.Sprintf("https://sbom.ubuntu-nix.system/%s", generateUUID()),
61-
CreationInfo: CreationInfo{
62+
DocumentNamespace: fmt.Sprintf("https://sbom.ubuntu-nix.system/%s", sbom.GenerateUUID()),
63+
CreationInfo: sbom.CreationInfo{
6264
Created: time.Now().UTC().Format(time.RFC3339),
6365
Creators: m.mergeCreators(ubuntuDoc, nixDoc),
6466
LicenseListVersion: "3.20",
6567
},
66-
Packages: []Package{},
67-
Relationships: []Relationship{},
68+
Packages: []sbom.Package{},
69+
Relationships: []sbom.Relationship{},
6870
}
6971

7072
// Create the single root System package
71-
systemPkg := Package{
73+
systemPkg := sbom.Package{
7274
SPDXID: "SPDXRef-System",
7375
Name: "Ubuntu-Nix-System",
7476
DownloadLocation: "NOASSERTION",
@@ -81,7 +83,7 @@ func (m *SBOMMerger) Merge(ubuntuPath, nixPath string) (*SPDXDocument, error) {
8183
mergedDoc.Packages = append(mergedDoc.Packages, systemPkg)
8284

8385
// Add document describes relationship
84-
mergedDoc.Relationships = append(mergedDoc.Relationships, Relationship{
86+
mergedDoc.Relationships = append(mergedDoc.Relationships, sbom.Relationship{
8587
SPDXElementID: "SPDXRef-DOCUMENT",
8688
RelatedSPDXElement: "SPDXRef-System",
8789
RelationshipType: "DESCRIBES",
@@ -102,7 +104,7 @@ func (m *SBOMMerger) Merge(ubuntuPath, nixPath string) (*SPDXDocument, error) {
102104
mergedDoc.Packages = append(mergedDoc.Packages, pkg)
103105

104106
// Add relationship to system root
105-
mergedDoc.Relationships = append(mergedDoc.Relationships, Relationship{
107+
mergedDoc.Relationships = append(mergedDoc.Relationships, sbom.Relationship{
106108
SPDXElementID: "SPDXRef-System",
107109
RelatedSPDXElement: pkg.SPDXID,
108110
RelationshipType: "CONTAINS",
@@ -127,7 +129,7 @@ func (m *SBOMMerger) Merge(ubuntuPath, nixPath string) (*SPDXDocument, error) {
127129
mergedDoc.Packages = append(mergedDoc.Packages, pkg)
128130

129131
// Add relationship to system root
130-
mergedDoc.Relationships = append(mergedDoc.Relationships, Relationship{
132+
mergedDoc.Relationships = append(mergedDoc.Relationships, sbom.Relationship{
131133
SPDXElementID: "SPDXRef-System",
132134
RelatedSPDXElement: pkg.SPDXID,
133135
RelationshipType: "CONTAINS",
@@ -140,21 +142,21 @@ func (m *SBOMMerger) Merge(ubuntuPath, nixPath string) (*SPDXDocument, error) {
140142
return mergedDoc, nil
141143
}
142144

143-
func (m *SBOMMerger) loadDocument(path string) (*SPDXDocument, error) {
145+
func (m *SBOMMerger) loadDocument(path string) (*sbom.SPDXDocument, error) {
144146
data, err := os.ReadFile(path)
145147
if err != nil {
146148
return nil, err
147149
}
148150

149-
var doc SPDXDocument
151+
var doc sbom.SPDXDocument
150152
if err := json.Unmarshal(data, &doc); err != nil {
151153
return nil, err
152154
}
153155

154156
return &doc, nil
155157
}
156158

157-
func (m *SBOMMerger) mergeCreators(ubuntuDoc, nixDoc *SPDXDocument) []string {
159+
func (m *SBOMMerger) mergeCreators(ubuntuDoc, nixDoc *sbom.SPDXDocument) []string {
158160
creatorMap := make(map[string]bool)
159161
var creators []string
160162

@@ -196,7 +198,7 @@ func (m *SBOMMerger) renumberSPDXID(originalID, prefix string) string {
196198
return fmt.Sprintf("SPDXRef-%s-%s", prefix, strings.TrimPrefix(originalID, "SPDXRef-"))
197199
}
198200

199-
func (m *SBOMMerger) Save(doc *SPDXDocument, outputPath string) error {
201+
func (m *SBOMMerger) Save(doc *sbom.SPDXDocument, outputPath string) error {
200202
file, err := os.Create(outputPath)
201203
if err != nil {
202204
return err

0 commit comments

Comments
 (0)